leetcode-107 二叉树的层次遍历 II
正常层序遍历,最后将数组翻转
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while (! q.isEmpty()){
List<Integer> list = new ArrayList<>();
int size = q.size();
for (int i = 0; i < size; i++){
TreeNode node = q.poll();
list.add(node.val);
if (node.left != null){
q.offer(node.left);
}
if (node.right != null){
q.offer(node.right);
}
}
res.add(list);
}
Collections.reverse(res);
return res;
}
}