116. 填充每个节点的下一个右侧节点指针
问题116 填充每个节点的下一个右侧节点指针 ★★☆☆☆ https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/ 这个题目其实就是二叉树层序遍历的变种,就是每一层从左到右维护成一个链表嘛,在层序遍历的基础上稍做修改即可。 public Node connect(Node root) { ...
问题116 填充每个节点的下一个右侧节点指针 ★★☆☆☆ https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/ 这个题目其实就是二叉树层序遍历的变种,就是每一层从左到右维护成一个链表嘛,在层序遍历的基础上稍做修改即可。 public Node connect(Node root) { ...
问题113 路径总和 ★★☆☆☆ https://leetcode-cn.com/problems/path-sum-ii/ 这个题目需要做的是遍历二叉树的每一条path,可以称为path遍历,而path遍历其实就是前序遍历,只不过我们需要在遍历过程中维护当前的path public List<List<Integer>> pathSum(TreeNode ro...
问题1123 最深叶子节点的最近公共祖先 ★★☆☆☆ https://leetcode.cn/problems/lowest-common-ancestor-of-deepest-leaves/ 题目本身并不难理解,一颗二叉树,每个节点都有一个depth,根节点depth为0,每往下每一层depth加1。这些节点中有一些是叶子节点,即没有孩子的节点。这些叶子节点可能有不同的depth,我们...
问题1110 删点成林 ★★☆☆☆ https://leetcode.cn/problems/delete-nodes-and-return-forest/ 这个题目并不难理解,但是刚入手时并没有什么好思路。首先想到的是递归,但是并不方便将大问题转化为子问题,也便作罢。还是从示例问题入手,尝试通过简单的例子寻找解题思路。以示例问题为例,当删除某个value的节点A时,需要做哪些事情呢? ...
问题107 二叉树的层序遍历 ★★☆☆☆ https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/ 额, 这个就在层序遍历的结果上加一个reverse就可以了,我觉得这道题比较多余。 public List<List<Integer>> levelOrderBottom(Tree...
问题106 从中序与后序遍历序列构造二叉树 https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 这个题目和问题105如出一辙,思路是一样的,不赘述。 public TreeNode buildTree(int[] inorder, int[] posto...
问题105 从前序与中序遍历序列构造二叉树 https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 根据前序遍历和中序遍历的特点,可知前序遍历的第一个节点是根节点,中序遍历中根节点左边是左子树,右边是右子树,由此可以递归的构造下去。我觉得这个思路还是比较好理解的,...
问题103 二叉树的锯齿形层序遍历 ★★☆☆☆ https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/ 这个题目就是把层序遍历稍微改动下,如果从入队列时的顺序入手考虑,感觉就很麻烦,其实就还是按层序遍历入队出队,根据当前层级,加一个reverse就好了。 public List<List...
问题1026 节点与其祖先之间的最大差值 ★★☆☆☆ https://leetcode.cn/problems/maximum-difference-between-node-and-ancestor/ 题目要求并不难理解,翻译一下,从root到每个叶子节点有一条path,这条path上最大值和最小值有一个差,现在要计算的是所有path中差最大的那一条对应的差是多少。 最直接粗暴的想法就...
问题102 二叉树的层序遍历 ★★☆☆☆ https://leetcode-cn.com/problems/binary-tree-level-order-traversal/ 这个题目就是层序遍历,不赘述。 public List<List<Integer>> levelOrder(TreeNode root) { if (root == null) { ...