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]]
Confused what "{1,#,2,3}"
means? > read more on how binary tree is serialized on OJ.
OJ ' s Binary Tree serialization:
The serialization of a binary tree follows a level order traversal, where ' # ' signifies a path terminator where no node ex Ists below.
Here's an example:
1 / 2 3 / 4 5
The above binary tree is serialized as
"{1,2,3,#,#,4,#,#,5}"
. Test instructions: traverse a tree backwards.
Idea: Still the same, the last inverted on the line
/** * Definition for a binary tree node. * public class TreeNode {* int val, * TreeNode left, * TreeNode right; * TreeNode (int x) {val = x;} *} */public class Solution {public list<list<integer>> levelorderbottom (TreeNode root) {list<list <Integer>> ans = new arraylist<list<integer>> (); if (root = null) return ans; queue<treenode> q = new linkedlist<treenode> (); Q.add (root); int count = 1, level = 0; while (!q.isempty ()) {list<integer> tmp = new arraylist<integer> (); level = 0; for (int i = 0; i < count; i++) {TreeNode t = q.poll (); Tmp.add (T.val); if (t.left! = null) {Q.add (t.left); ++level; } if (t.right! = null) {Q.add (t.right); ++level; }} count = level; Ans.add (TMP); } collections.reverse (ANS); return ans; }}
Leetcode Binary Tree level Order traversal II