Find Bottom Left Tree Value

Given the `root` of a binary tree, return the leftmost value in the last row of the tree.

30:00

Find Bottom Left Tree Value
medium
Free written solution
Topics
Companies

Given the root of a binary tree, return the leftmost value in the last row of the tree.

Example 1:
Input: {"root":[2,1,3]}
Output: 1
Constraints:
  • The number of nodes in the tree is in the range [1,104][1, 10^4].

  • −231≤Node.val≤231−1-2^{31} \leq \text{Node.val} \leq 2^{31} - 1

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

Run code to see results.

Input
arr ={"root":[2,1,3]}

Run a breadth-first search, but enqueue the right child before the left. Each row then leaves the queue from right to left, so the very last node out is the leftmost node of the deepest row.

row0sweep right to leftrow1213

queue (front on the left)

front2

dequeue order (the last one is the answer)

nothing yet

node (just dequeued)
waiting in queue
dequeued, with order number
last out = bottom-left
Variables
VariableValue
node2
queue[2]
popped0
DepthFunction Call
Stack empty
0/7