DFS
文章平均质量分 67
Dylan_Java_NYC
练很重要,总结更重要,感谢CSDN给了我这么好的平台交流。
展开
-
LeetCode Flatten Binary Tree to Linked List
原题链接在这里:https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Method 1: 前序遍历,都加到List里,最后从List加到树里。Time O(n), Space O(n). Method 2: 在做Pre-order时更改树的结构,使用stack特性,每次出栈时加到lastVisit的右子点,同时更新l原创 2015-09-09 08:03:42 · 267 阅读 · 0 评论 -
LeetCode Path Sum II
原题链接在这里:https://leetcode.com/problems/path-sum-ii/ 递归调用,终止条件是当遇到叶子节点时判断sum是否为0,则res加当前ls. 若root.left 不为空,则ls.add(root.left.val)然后递归调用helper, 用完后要remove掉尾节点。右侧相同。 Note: 1. 当res加 ls时一定要res.add(new Arr原创 2015-09-07 04:14:33 · 304 阅读 · 0 评论 -
LeetCode Binary Tree Paths
原题链接在这里:https://leetcode.com/problems/binary-tree-paths/ DFS 依次添加,终止条件是叶子节点。 Note: 1. 如果使用StringBuilder, 在递归调用时,必须写成new StringBuilder(sb). 否则会报错. e.g. [1,2,3], 会写成["1->2","1->23"]. 2 . 但如果使用String原创 2015-09-06 12:53:47 · 392 阅读 · 1 评论 -
LeetCode Binary Tree Maximum Path Sum
原题链接在这里:https://leetcode.com/problems/binary-tree-maximum-path-sum/ maxVal中包含着最后要返回的结果。 helper函数是求当前root往下的最大路径,先算左边路径,返回值直接和0比较,若不是正数就返回0,然后同样方法算右边路径。 当前最深路径看当前root的val加上左边或者右边,哪个大,helper函数返回大的那个。原创 2015-09-11 07:09:34 · 295 阅读 · 0 评论 -
LeetCode Symmetric Tree
原题链接在这里:https://leetcode.com/problems/symmetric-tree/ 本题与Same Tree类似。这里比较是否symmetric,也是用recursion,需要写一个helper,递归调用,不过每次对称着比较。 终止条件:同为null, return true; one is null, the other is not, return false; 同原创 2015-09-05 02:06:52 · 269 阅读 · 0 评论 -
LeetCode Same Tree
原题链接在这里:https://leetcode.com/problems/same-tree/ 树的题目多用递归可以简单地写出来。 终止条件:若左右都为空就return true; 若一个为空另一个不为空 return false; 最后两个都不为空时,值不同return false. Time O(n), Space O(logn). Note: 1. 左右都不为空,值相同时继续rec原创 2015-09-05 01:24:37 · 282 阅读 · 0 评论 -
LeetCode Maximum Depth of Binary Tree
原题链接在这里:https://leetcode.com/problems/maximum-depth-of-binary-tree/ 求左右最大深度recursively, 比较出最大的再加一,终止条件是root == null, 返回0. 进阶题目是Balanced Binary Tree. AC Java: /** * Definition for a binary tree no原创 2015-09-05 00:49:42 · 295 阅读 · 0 评论 -
LeetCode Minimum Depth of Binary Tree
原题链接在这里:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 本题与Maximum Depth of Binary Tree相似,不同是需要判断叶子节点,若是给出[1,2]这种情况,返回值应该是2而不是1,root的右节点为空,而根据定义:The minimum depth is the number of nod原创 2015-09-05 01:07:36 · 250 阅读 · 0 评论 -
LeetCode Balanced Binary Tree
原题链接在这里:https://leetcode.com/problems/balanced-binary-tree/ 用递归方法求最大深度,如果左右最大深度相差大于一,则返回-1,若是已有左或者右的返回值为-1,则立即返回-1. 最后看返回到root时是否为一个负数,若是负数则不是balanced,若是正数,则返回了最大深度,是balanced. AC Java: /** * Defi原创 2015-09-05 00:44:43 · 307 阅读 · 0 评论 -
LeetCode Kth Smallest Element in a BST
原题链接在这里:https://leetcode.com/problems/kth-smallest-element-in-a-bst/ Method 1: BST 用in-order遍历出来的就是由小到大,Method1用recursion,会出来一整个list, 然后返回list.get(k-1)即可。 AC Java: /** * Definition for a binary tr原创 2015-09-09 09:08:29 · 273 阅读 · 0 评论 -
Java Binary Tree DFS
DFS Recursion: public void DFS(TreeNode root){ if(root == null){ return; } System.out.println(root.val); DFS(root.left); DFS(root.right); }原创 2015-09-06 12:02:49 · 389 阅读 · 0 评论 -
LeetCode Count Complete Tree Nodes
原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ 递归调用函数,终止条件两个,一个是root == null, return 0, 一个是左右高度相同说明是满树,return 2^height-1。 若是左右高度不同,递归调用求左子树包含Node数+右子树包含Node数+1(自身)。 Note: 1. 用Math原创 2015-09-09 00:48:15 · 318 阅读 · 0 评论 -
LeetCode Sum Root to Leaf Numbers
原题链接在这里:https://leetcode.com/problems/sum-root-to-leaf-numbers/ 本题与Path Sum II相似,都是递归调用。终止条件都是左右child都是空,否则cur*10加child.val, 递归调用函数,再去掉尾节点。 Note: 1. helper 是pass by value, 所以res一定要用个array存储。 2. 递原创 2015-09-07 22:29:32 · 289 阅读 · 0 评论