正常level-order traversal,每层到一个array里 反向打印就用Linklist的descendingIterator, 或者用stack存每层的值就行。\
import java.util.*;
public class Solution {
public int[][] levelOrderBottom (TreeNode root) {
if (root == null) return new int[0][];
LinkedList<int[]> list = new LinkedList<>();
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while (!q.isEmpty()) {
int levelSize = q.size();
int[] level = new int[levelSize];
for (int i = 0; i < levelSize; ++i) {
TreeNode head = q.poll();
if (head.left != null) q.offer(head.left);
if (head.right != null) q.offer(head.right);
level[i] = head.val;
}
list.add(level);
}
Iterator<int[]> reverseIt = list.descendingIterator();
int[][] ans = new int[list.size()][];
int i = 0;
while (reverseIt.hasNext()) {
ans[i++] = reverseIt.next();
}
return ans;
}
}