题目:
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]
]
思路:
递归,然后swap
package treetraversal; import java.util.ArrayList;
import java.util.List; public class BinaryTreeLevelOrderTraversalII { public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
levelOrderTraversal(res, root, 0);
int n = res.size();
for (int i = 0; i < n / 2; ++i) {
List<Integer> tmp = res.get(i);
res.set(i, res.get(n - i - 1));
res.set(n - i - 1, tmp);
}
return res;
} private void levelOrderTraversal(List<List<Integer>> res, TreeNode root, int k) {
if (root == null) return;
if (res.size() < k + 1) {
List<Integer> record = new ArrayList<Integer>();
record.add(root.val);
res.add(record);
} else {
res.get(k).add(root.val);
}
levelOrderTraversal(res, root.left, k + 1);
levelOrderTraversal(res, root.right, k + 1);
} }