Given a binary tree, return the bottom-up level order traversal of its nodes ' values. (ie, from the left-to-right, the level by level from the leaf to root).
For example:
Given binary Tree {3,9,20,#,#,15,7} ,
3 / 9 / 7
Return its bottom-up level order traversal as:
[ [15,7], [9,20], [3]]
Problem Solving Ideas:
Modify the Java for Leetcode 102 Binary Tree level Order traversal can be resolved, Java implementation is as follows:
Public list<list<integer>> Levelorderbottom (TreeNode root) { list<list<integer>> List = New Arraylist<list<integer>> (); if (root = null) return list; queue<treenode> queue = new linkedlist<treenode> (); Queue.add (root); while (Queue.size ()! = 0) { list<integer> alist = new arraylist<integer> (); for (TreeNode child:queue) alist.add (child.val); List.add (New arraylist<integer> (alist)); Queue<treenode> Queue2=queue; Queue=new linkedlist<treenode> (); for (TreeNode child:queue2) { if (child.left! = null) queue.add (child.left); if (child.right! = null) queue.add (child.right);} } Collections.reverse (list); return list; }
Java for Leetcode 107 Binary Tree level Order traversal II