/**
* 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 LinkedList<>();
Queue<TreeNode> q = new LinkedList<>();
if (root == null) return res;
q.offer(root);
while(!q.isEmpty()) {
List<Integer> temp = new LinkedList<>();
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode node = q.poll();
temp.add(node.val);
if (node.left != null)
q.offer(node.left);
if (node.right != null)
q.offer(node.right);
}
res.add(temp);
}
Collections.reverse(res);
return res;
}
}
leecode-107-二叉树的层次遍历 II
最新推荐文章于 2022-08-03 16:03:06 发布