Binary Tree Level Order Traversal II

Medium Trees & BST Traversals Original on LeetCode

Binary Tree Level Order Traversal II is a medium trees & bst problem solved with the traversals pattern. The best approach, bfs, adding each level at the front, runs in O(n) time and O(w) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from bfs, then reverse the list of levels up.

Problem

Return the level order traversal from the bottom level up to the root, left to right within each level.

Examples

Example 1

Input
root = [3, 9, 20, null, null, 15, 7]
Output
[[15, 7], [9, 20], [3]]

Example 2

Input
root = [1]
Output
[[1]]

Constraints

  • The tree has 0 to 2000 nodes.

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 10 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
BFS, then reverse the list of levelsO(n)O(w)
BFS, adding each level at the frontO(n)O(w)

1BFS, then reverse the list of levels

TimeO(n)
SpaceO(w)

Run normal level order and reverse the outer list at the end.

  1. Standard BFS; Collections.reverse(out).
class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> out = new ArrayList<>();
        if (root == null) return out;
        Queue<TreeNode> q = new ArrayDeque<>();
        q.add(root);
        while (!q.isEmpty()) {
            List<Integer> level = new ArrayList<>();
            for (int i = q.size(); i > 0; i--) {
                TreeNode n = q.poll();
                level.add(n.val);
                if (n.left != null) q.add(n.left);
                if (n.right != null) q.add(n.right);
            }
            out.add(level);
        }
        Collections.reverse(out);
        return out;
    }
}

2BFS, adding each level at the front

TimeO(n)
SpaceO(w)

Insert each level at index 0 of a LinkedList, so the deepest level ends up first with O(1) insertion.

  1. out = new LinkedList; out.addFirst(level) after each level.
class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        LinkedList<List<Integer>> out = new LinkedList<>();
        if (root == null) return out;
        Queue<TreeNode> q = new ArrayDeque<>();
        q.add(root);
        while (!q.isEmpty()) {
            List<Integer> level = new ArrayList<>();
            for (int i = q.size(); i > 0; i--) {
                TreeNode n = q.poll();
                level.add(n.val);
                if (n.left != null) q.add(n.left);
                if (n.right != null) q.add(n.right);
            }
            out.addFirst(level);
        }
        return out;
    }
}

Edge cases to test

  • Empty tree

Hints

Hint 1

Same BFS; only the order you add levels to the answer changes.

FAQ

What is the best time complexity for Binary Tree Level Order Traversal II?

BFS, adding each level at the front runs in O(n) time and O(w) extra space.

Which pattern does Binary Tree Level Order Traversal II use?

It is a trees & bst problem that uses the traversals pattern. Other problems with the same pattern: Binary Tree Level Order Traversal, Binary Tree Zigzag Level Order Traversal, Populating Next Right Pointers in Each Node.

Is there a brute force solution for Binary Tree Level Order Traversal II?

Yes. BFS, then reverse the list of levels takes O(n) time and O(w) space. Run normal level order and reverse the outer list at the end.

Which edge cases should I test for Binary Tree Level Order Traversal II?

Empty tree.