Paste_Image.png

# 代码

```/**
* Definition of TreeNode:
* public class TreeNode {
*     public int val;
*     public TreeNode left, right;
*     public TreeNode(int val) {
*         this.val = val;
*         this.left = this.right = null;
*     }
* }
*/

public class Solution {
/**
* @param root: The root of binary tree.
* @return: buttom-up level order a list of lists of integer
*/
public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
if(root == null)
return new ArrayList<>();

ArrayList<ArrayList<Integer>> res = new ArrayList<>();

list.offer(root);

while(!list.isEmpty()) {
int size = list.size();
ArrayList<Integer> level = new ArrayList<>();
for(int i=0;i<size;i++) {
TreeNode cur = list.poll();
if(cur.left != null)
list.offer(cur.left);
if(cur.right != null)
list.offer(cur.right);
}
}
Collections.reverse(res);
return res;
}
}```

0 条评论

## 相关文章

1173

732

### LeetCode Maximum Depth of Binary Tree题目分析代码

Given a binary tree, find its maximum depth.

631

1442

722

### 剑指Offer-按之字形顺序打印二叉树

package Tree; import java.util.ArrayList; import java.util.LinkedList; import j...

2834

### 剑指Offer-二叉树的镜像

package Tree; import java.util.ArrayDeque; import java.util.ArrayList; import j...

3334

872

2623

1585