题目:
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from 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] ]
解答一:
使用队列进行水平遍历,每行结果放到栈里,再弹出到List里。
代码:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
public class Solution {
public class NewNode {
TreeNode node;
int level;
NewNode(TreeNode m, int l) {
node = m;
level = l;
}
}
public List<List<Integer>> levelOrderBottom(TreeNode root) {
if (root == null)
return new Vector<List<Integer>>();
Queue<NewNode> nodeQueue = new LinkedList<NewNode>();
Vector<Integer> cur = new Vector<Integer>();
Stack<Vector<Integer>> retStack = new Stack<Vector<Integer>>();
int lastLevel = 0;
nodeQueue.offer(new NewNode(root,0));
while (!nodeQueue.isEmpty()) {
NewNode head = nodeQueue.poll();
if (head.level > lastLevel) {
retStack.push(cur);
cur = new Vector<Integer>();
lastLevel = head.level;
}
cur.add(head.node.val);
if (head.node.left != null) {
nodeQueue.offer(new NewNode(head.node.left, lastLevel + 1));
}
if (head.node.right != null) {
nodeQueue.offer(new NewNode(head.node.right, lastLevel + 1));
}
}
retStack.push(cur);
List<List<Integer>> ret = new Vector<List<Integer>>();
while (!retStack.isEmpty()) {
ret.add(retStack.pop());
}
return ret;
}
}
解答二:
用递归进行水平遍历,结果保存到List<List<Integer>>对象里,再逆序输出到返回值里
代码:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
public class Solution {
public List<List<Integer> > result = new Vector<List<Integer> >();
public void levelTraversal(TreeNode root,int level){
if(root == null)
return;
if(level == result.size()){
result.add(new Vector<Integer>());
}
result.get(level).add(root.val);
levelTraversal(root.left,level+1);
levelTraversal(root.right,level+1);
}
public List<List<Integer>> levelOrderBottom(TreeNode root){
levelTraversal(root,0);
List<List<Integer>> ret = new Vector<List<Integer>>();
for(int i=result.size();i>0;i--){
ret.add(result.get(i-1));
}
return ret;
}
}