You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
package class07;
|
|
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Queue;
|
|
|
|
|
|
|
|
|
|
// 测试链接:https://leetcode.com/problems/binary-tree-level-order-traversal-ii
|
|
|
|
|
public class Code01_BinaryTreeLevelOrderTraversalII {
|
|
|
|
|
|
|
|
|
|
public static class TreeNode {
|
|
|
|
|
public int val;
|
|
|
|
|
public TreeNode left;
|
|
|
|
|
public TreeNode right;
|
|
|
|
|
|
|
|
|
|
TreeNode(int val) {
|
|
|
|
|
this.val = val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<List<Integer>> levelOrderBottom(TreeNode root) {
|
|
|
|
|
List<List<Integer>> ans = new LinkedList<>();
|
|
|
|
|
if (root == null) {
|
|
|
|
|
return ans;
|
|
|
|
|
}
|
|
|
|
|
Queue<TreeNode> queue = new LinkedList<>();
|
|
|
|
|
queue.add(root);
|
|
|
|
|
while (!queue.isEmpty()) {
|
|
|
|
|
int size = queue.size();
|
|
|
|
|
List<Integer> curAns = new LinkedList<>();
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
TreeNode curNode = queue.poll();
|
|
|
|
|
curAns.add(curNode.val);
|
|
|
|
|
if (curNode.left != null) {
|
|
|
|
|
queue.add(curNode.left);
|
|
|
|
|
}
|
|
|
|
|
if (curNode.right != null) {
|
|
|
|
|
queue.add(curNode.right);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ans.add(0, curAns);
|
|
|
|
|
}
|
|
|
|
|
return ans;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|