leetcode summary 09/03

270. Closest Binary Search Tree Value recursive: pass pre node value when null iterative: similar dfs 272. Closest Binary Search Tree Value II inorder traverse all nodes and get k nearest 285. Inorder Successor in BST read carefully 298. Binary Tree Longest Consecutive Sequence dfs: pass parent value to children and compare values 333. Largest BST Subtree construct a class to hold size, lower bound, upper bound go in bottom-up deal with leaves carefully, better use infinit

leetcode summary 09/02 (10)

617. Merge Two Binary Trees dfs 606. Construct String from Binary Tree right can be omitted in some case 553. Optimal Division math: x1 must be numerator and x2 must go to denominator. 545. Boundary of Binary Tree wrong answer: [37,-34,-48,null,-100,-100,48,null,null,null,null,-54,null,-71,-22,null,null,null,8] [1] [0, null, 0, 0] neat solution of one traversal: node.left is left bound if node is left bound; node.

leetcode summary 09/01

662. Maximum Width of Binary Tree iterative: save index of each node while queue nodes. recursive: dfs, keep a array of left most nodes. 640. Solve the Equation corner cases: “0x=0” “x=-x” “x=-1” “0x=-x”

Java::Comparator

做题的时候遇到了sort,想试试Java 8的新语法。于是有了一些研究: Reference Very confused by Java 8 Comparator type inference access field by lambda. intervals is a list of interval. intervals.sort(Comparator.comparingInt((Interval i) -> i.start)); Comparator.comparing((Person p)->p.firstName) .thenComparing(p->p.lastName) .thenComparingInt(p->p.age); Comparator.comparing(Person::getFirstName) .thenComparing(Person::getLastName) .thenComparingInt(Person::getAge); @Override public int compareTo(Person o){