"107-binary Tree Level Order Traversal II (binary tree Sequence Traversal II)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
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 20 / 15 7
Return its bottom-up level order traversal as:
[ [15,7], [9,20], [3]]
Main Topic
Given a binary tree, the sequence traversal is done from the bottom down.
Thinking of solving problems
The tree is sequentially traversed, and the results of each layer are placed on the head of the resulting list.
Code Implementation
publicclass TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}
Algorithm implementation class
ImportJava.Util.*; PublicClass Solution { Public List<List<Integer>>Levelorderbottom (TreeNode root) {List<List<Integer>> List = NewLinkedList<>();if(Root== NULL) {return List; } Deque<TreeNode>Cur= NewLinkedList<>(); Deque<TreeNode>Nxt= NewLinkedList<>(); Deque<TreeNode>Exc= NewLinkedList<>(); TreeNode tmp; Cur.Add (root); while(!Cur.IsEmpty ()) {List<Integer>Layout= NewArrayList<>(); while(!Cur.IsEmpty ()) {tmp=Cur.Remove ();if(TMP.Left!= NULL) {NXT.Add (tmp.left); }if(TMP.Right!= NULL) {NXT.Add (tmp.right); } layout.Add (tmp.Val); } exc=Cur Cur=Nxt Nxt=ExcList.Add0, layout); }return List; }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47392965"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java implementation" "107-binary Tree Level Order Traversal II (binary sequence Traversal II)"