Break the loop at the last node which pointed to the entry.
Given a binary tree, collect a tree ' s nodes as if you were doing this:collect and remove all leaves, repeat until the Tre E is empty.
Example:
Given binary Tree
1 / 2 3 /\ 4 5
Returns [4, 5, 3], [2], [1].
ollowup: Each node still has a left,right pointer, but the structure is not a tree, but graph how to calculate, consider there is/no circular dependency two kinds of situations;the method is to record the calculated node level and a set record with HashMap dependent node
Public list<list<integer>> findleaves (TreeNode root) { list<list<integer>> result = new Arraylist<list<integer>> (); Helper (result, root); return result;} Traverse the tree bottom-up recursivelyprivate int helper (list<list<integer>> List, TreeNode root) { if (root==null) return-1; int left = helper (list, root.left); int right = Helper (list, root.right); int curr = Math.max (left, right) +1; The first time this code is reached was when curr==0, //since The tree was bottom-up processed. if (List.size () <=curr) { list.add (new arraylist<integer> ()); } List.get (Curr). Add (root.val); return Curr;}
Linked list focus problem, a lot of polygon, consider intersect disjoint, ring no ring + Find Leaves of Binary Tree (Java)