Binary Tree Level Order Traversal II
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]
]
跟上题一样 只不过是倒着输出
代码
public class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
LinkedList<List<Integer>> list = new LinkedList<List<Integer>>();
helper(list, 0, root);
return list;
}
public void helper(LinkedList<List<Integer>> list, int height, TreeNode node){
if(node == null) return;
if(list.size() - 1 < height){
list.addFirst(new LinkedList<Integer>());
}
list.get(list.size() - 1 - height).add(node.val);
helper(list, height+1, node.left);
helper(list, height+1, node.right);
}
}