
Recursion
文章平均质量分 73
Dylan_Java_NYC
练很重要,总结更重要,感谢优快云给了我这么好的平台交流。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode Reverse Linked List
原题链接在这里:https://leetcode.com/problems/reverse-linked-list/Method 1 和 Method 2 都是用的Iteration.Method 1 是建一个dunmy,从前往后扫原链表,遇到一个就用改点的值建一个新的Node加在dunmy和dunmy.next之间。Time O(n), Space O(n), 因为建了一个新的lis原创 2015-08-22 04:15:48 · 754 阅读 · 0 评论 -
LeetCode Binary Tree Preorder Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-preorder-traversal/虽然原题要求不让用recursion,但还是试了一下。一试发现自己的recursion真心无语。Method 1 是Recursion,学到的新方法就是若是要在recursion中维护一个生成的变量,可以再造一个函数,然后把这个要维护的变量当成argum原创 2015-08-19 09:15:12 · 581 阅读 · 0 评论 -
LeetCode Binary Tree Inorder Traversal
原题链接在这里:https://leetcode.com/problems/binary-tree-inorder-traversal/#本题与Binary Tree Preorder Traversal相呼应。可以分别采用Recursion, Iteration 和 Morris Traversal 三种方法。Method 1: RecursionRecursio原创 2015-08-20 02:48:01 · 380 阅读 · 0 评论 -
LeetCode Unique Binary Search Trees II
原题链接在这里:https://leetcode.com/problems/unique-binary-search-trees-ii/这道题是Unique Binary Search Trees的进阶版本. 返回的不是个数,而是每一个结果。循环中递归调用helper函数求解子问题,以i为root时,BST的left child 是由1到i-1生成的,BST的right child 是由i原创 2015-09-11 03:11:21 · 372 阅读 · 0 评论 -
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 · 344 阅读 · 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 · 314 阅读 · 0 评论 -
LeetCode Validate Binary Search Tree
原题链接在这里:https://leetcode.com/problems/validate-binary-search-tree/根据BST特性递归调用原函数,如果出现root.val > max 或者root.val 本来这道题并不难,但加上了边界处理就太让人无语了。Note: 1. min, max 需用long型,如果不用long, 会有 [-2147483648,-21474原创 2015-09-10 23:05:07 · 396 阅读 · 0 评论 -
LeetCode Construct Binary Tree from Preorder and Inorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/利用preorder 和 inorder来构造树。e.g. preorder 1, 2, 4, 5, 3, 6, 7 inorder 4, 2, 5, 1, 6, 3, 7先原创 2015-08-21 02:33:55 · 546 阅读 · 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 · 406 阅读 · 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 · 463 阅读 · 1 评论 -
LeetCode Construct Binary Tree from Inorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/这道题与Construct Binary Tree from Preorder and Inorder Traversal 思路相似,不同之处在于这里的root在postorder的最有一位,其他都相同原创 2015-08-21 03:03:10 · 414 阅读 · 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 · 345 阅读 · 0 评论 -
LeetCode Word Break II
原题链接在这里:https://leetcode.com/problems/word-break-ii/本题是Word Break的进阶题,要返回所有wordbreak的结果。本题参照了这篇帖子:http://www.cnblogs.com/springfor/p/3877056.html首先更具isWordBreak判断是否能拆,若能拆看从头到尾走,找到了一个wordDict里包含的原创 2015-09-13 01:40:09 · 538 阅读 · 0 评论 -
LeetCode Palindrome Partitioning
原题链接在这里:https://leetcode.com/problems/palindrome-partitioning/本题与Word Break II相似。用递归解决子问题,如果是Palindrome就加进item,然后递归调用helper, 递归终止条件是能正好走到s.length().Note: 1. 用的item是一个list, 实际上是一个指针, 所以递归调用完需要回朔,原创 2015-09-13 02:22:21 · 338 阅读 · 0 评论 -
LeetCode Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/是Lowest Common Ancestor of a Binary Search Tree的进阶题目。无法比较大小,但是可以看p,q是不是在root的两边,若在两边,则返回root. 若都在一边,就在那一边继续。Note: 1.原创 2015-09-05 05:11:20 · 448 阅读 · 0 评论 -
LeetCode Invert Binary Tree
原题链接在这里:https://leetcode.com/problems/invert-binary-tree/树的问题多为recursion. 从上到下一层一层交换左右。Time O(n), Space O(logn).Note: 原函数返回TreeNode, 可以来一个helper返回void来辅助recursion. 若是不想要返回值,单纯想改变原结构时可以用这种建一个返回vo原创 2015-09-05 02:27:44 · 410 阅读 · 0 评论 -
LeetCode Sudoku Solver
原题链接在此:https://leetcode.com/problems/sudoku-solver/这道题是递归回溯. Note: 1. 设值新的helper recursive function 要带有返回boolean 值,因为要判断此递归方案正确与否。 2. 检查本轮时候正确后还需检查下一轮,这里还是有点不明白。 3. 注意什么时候返回。先要枚举所有本轮的解,对于每原创 2015-07-22 09:12:35 · 391 阅读 · 0 评论 -
LeetCode Sort List
原题链接在这里:https://leetcode.com/problems/sort-list/思路: 1. 递归采用merge sort2. 通过midList找到中点,从中点和中点后一点断开3. 前后段分别递归merge sort,一直拆到剩一个点,然后再mergeNote: 1. 必须记得要断开原有list,否则会栈溢出。2. 若有偶数个点,midList找的是中间一对前原创 2015-09-03 02:59:34 · 427 阅读 · 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 · 329 阅读 · 0 评论 -
LeetCode Convert Sorted Array to Binary Search Tree
原题链接在这里:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/本题与Convert Sorted List to Binary Search Tree相似,不同就是这个argument是array. 所以为了方便记左右index来做递归,就建一个有多个argument的函数。这也是array原创 2015-09-03 04:04:11 · 359 阅读 · 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 · 384 阅读 · 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 · 440 阅读 · 0 评论 -
LeetCode Convert Sorted List to Binary Search Tree
原题链接在这里:https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/找到中点当BST的root,如此递归调用,知道只有一个点时返回由当前点生成的TreeNode,以此设为终止条件。Note: 1. 此处midList找到的是中点的前一个点,然后拆成三段,中点前一段,中点单独一个,中点后一段。2.原创 2015-09-03 03:36:29 · 517 阅读 · 0 评论 -
LeetCode Populating Next Right Pointers in Each Node
原题链接在这里:https://leetcode.com/problems/populating-next-right-pointers-in-each-node/递归调用,如果root.next不为null, root.right.next借助于root.next.left, 然后root.left.next指向root.right.若是算上递归,Space O(logn).AC J原创 2015-09-10 02:48:41 · 481 阅读 · 0 评论 -
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 · 322 阅读 · 0 评论 -
LeetCode Recover Binary Search Tree
原题链接在这里:https://leetcode.com/problems/recover-binary-search-tree/采用inorder遍历BST应该是返回从小到大的顺序,但这里有两个点顺序错了,所以不会完全从小到大。如果两个相邻点顺序错了,inorder就应该有一个地方大小顺序颠倒,如果不是相邻点顺序错了,inorder应有两个地方大小顺序颠倒。e.g. 1234567,原创 2015-09-10 07:04:09 · 459 阅读 · 0 评论 -
LeetCode Populating Next Right Pointers in Each Node II
原题链接在这里:https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/本题是Populating Next Right Pointers in Each Node的进阶版。关键是在有null的时候如何找到下一节点,通过while loop找到next节点,while loop 很关键。N原创 2015-09-10 04:52:12 · 558 阅读 · 0 评论 -
LeetCode Lowest Common Ancestor of a Binary Search Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/递归调用,如果p, q的值都小于root.val就在root.left中继续找,如果p, q的值都大于root.val就在root.right中继续找。只有当root的val在p, q的val中间包括p, q的val时,返原创 2015-09-05 04:52:07 · 353 阅读 · 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 · 383 阅读 · 0 评论 -
LeetCode Pow(x, n)
原题链接在这里:https://leetcode.com/problems/powx-n/这道题和Sqrt(x)以及Divide Two Integers都是原有公式的题。这类题目一般用二分法(Sqrt(x))和以2为基地accumulation表示法(Divide Two Integers)都是为了节省时间到O(logn).这里采用了二分法,建立一个helper返回n为正数时的p原创 2015-09-15 03:00:17 · 317 阅读 · 0 评论