Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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 List<List<Integer>> levelOrderBottom(TreeNode root) {
- List<List<Integer>> result = new ArrayList<>();
- if (root == null) {
- return result;
- }
- Queue<TreeNode> queue = new LinkedList<TreeNode>();
- queue.offer(root);
- while (!queue.isEmpty()) {
- int size = queue.size();
- List<Integer> level = new ArrayList<>();
- for (int i = 0; i < size; i++) {
- TreeNode head = queue.poll();
- level.add(head.val);
- if (head.left != null) {
- queue.offer(head.left);
- }
- if (head.right != null) {
- queue.offer(head.right);
- }
- }
- result.add(level);
- }
- Collections.reverse(result);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement