摘要:This is very similar to Binary Tree Level Order Traversal. Just reverse the order of the list. ArrayList has API- > add(int index, Element) or Collect
阅读全文
摘要:Note: This question is good summary for this kind of problem. 1) Once you get the root, loop through all the children. Get the max up/down/max from th
阅读全文
摘要:Note: O(n) This question is the extension of the version one. The longest consecutive Sequence could be found in "Parent->Children" or "Children -> Pa
阅读全文
摘要:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { ...
阅读全文
摘要:Note: This is question is very similar to LCA original. The only difference is that the node may not exist. So if the node is not exsit, of course the
阅读全文
摘要:Note 这道题和一不同,给了一个Node向上回溯的可能,所以不需要recersive的找。因为之前的那个题不能回头,所以必须先到最下面(或者找的A和B)。这道题我们只需要把A和B的path记住就可以了,然后比较path中从root到A或者B,一直到开始不一样的时候停止,那个最后一个一样的就是LCA
阅读全文
摘要:Note: // 在root为根的二叉树中找A,B的LCA: // 如果找到了就返回这个LCA // 如果只碰到A,就返回A // 如果只碰到B,就返回B // 如果都没有,就返回null Just consider relationship between left, right and root
阅读全文
摘要:Note: It is easier to use divided conquer. As you can see, the question is just to add right to left's last. Here we care more about last then the top
阅读全文
摘要:Node: Quick Select: Avg O(N) Thought: put all the element less(more) than pivot to the first part, the rest to the second part. And Kth only falls to
阅读全文
摘要:Important: using a/b < c/d => a*d < c*b to check, we don't need to do the type transfer For the tree, if we need to get the max/min tree node, it is b
阅读全文
摘要:class Solution { /** * @param A: sorted integer array A which has m elements, * but size of A is m+n * @param B: sorted integer array B which has n elements * @retu...
阅读全文
摘要:class Solution { /** * @param A and B: sorted integer array A and B. * @return: A new sorted integer array */ public int[] mergeSortedArray(int[] A, int[] B) { // Write...
阅读全文
摘要:Note:a balaced tree needs to be balaced all the time. So it is important remember the status of each subtree. If it is not balaced in the middle, the
阅读全文
摘要:/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left ...
阅读全文
摘要:/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left ...
阅读全文
摘要:This question is a little bit harder than the Max Depth. We only compare the depth of the leaf node. If it is missing one side, this level shouldn't b
阅读全文
摘要:public class Solution { /** * @param A an integer array * @return void */ public void sortIntegers2(int[] A) { // Write your code here if (A == null || A.length...
阅读全文
摘要:O(n) worst, O(nlogn) average. Use quickSort This is quickSort
阅读全文
摘要:Note: 审题: 注意也有一种情况是: 一共有N个元素,要k个,但有不需要从每一个块去取, 只需要从其中的一些取。所以这里end开始应该取最大的element。而且有可能完全取不到,所以start应该从0开始取。剩下的和copy book那道题想法一致。
阅读全文