Binary Tree Level Order Traversal II

Given the root of a binary tree, return the bottom-up level order traversal of its node values: each level read from left to right, starting with the deepest level and ending with the root.

30:00

Binary Tree Level Order Traversal II
medium
Free written solution
Topics
Companies

Given the root of a binary tree, return the bottom-up level order traversal of its node values: each level read from left to right, starting with the deepest level and ending with the root.

Example 1:
Input: {"root":[3,9,20,null,null,15,7]}
Output: [[15,7],[9,20],[3]]
Constraints:
  • The number of nodes in the tree is in the range [0,2000][0, 2000].

  • −1000≤Node.val≤1000-1000 \leq \text{Node.val} \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"root":[3,9,20,null,null,15,7]}

Make res a deque so each finished level can be added to its front in O(1).

d0d1d23920157

queue

front

empty

level

[]

res

each finished level is inserted at the front (top)

[]

level being read
in queue (next level)
in res
Variables
VariableValue
q[]
level[]
levels0
DepthFunction Call
Stack empty
0/19