Bird
Raised Fist0
Interview Preptree-bfsmediumAmazonGoogle

Find Bottom Left Tree Value

Choose your preparation mode4 modes available
📋
Problem

Imagine you are looking at a tree from the bottom left side and want to find the value of the node that would be visible there.

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

The number of nodes in the tree is in the range [1, 10^5].Node values are integers and can be positive, negative, or zero.
Edge cases: Single node tree → output is the root valueTree with only left children → output is the bottom-most left childTree with only right children → output is the bottom-most right child (which is also leftmost at that level)
</>
IDE
def findBottomLeftValue(root: TreeNode) -> int:public int findBottomLeftValue(TreeNode root)int findBottomLeftValue(TreeNode* root)function findBottomLeftValue(root)
def findBottomLeftValue(root):
    # Write your solution here
    pass
class Solution {
    public int findBottomLeftValue(TreeNode root) {
        // Write your solution here
        return 0;
    }
}
#include <vector>
using namespace std;

int findBottomLeftValue(TreeNode* root) {
    // Write your solution here
    return 0;
}
function findBottomLeftValue(root) {
    // Write your solution here
}
Coming soon
0/9
Common Bugs to Avoid
Wrong: root value instead of bottom leftNot traversing beyond root or not updating leftmost node at deeper levels.✅ Update leftmost value at the start of each BFS level, not just once at root.
Wrong: left child of root instead of bottom leftGreedy approach assuming bottom left is always left child of root or first left child found.✅ Use BFS to traverse all levels and update leftmost node at each level.
Wrong: rightmost node at last levelNot tracking leftmost node correctly, possibly updating with last node at level.✅ Record the first node at each level during BFS, not the last.
Wrong: incorrect node due to off-by-one depth errorUpdating leftmost node at wrong depth or not strictly greater depth.✅ Update leftmost only when current depth > max_depth.
Wrong: TLE or stack overflowUsing DFS with exponential complexity or inefficient recursion on large input.✅ Implement BFS with queue for O(n) time complexity.
✓
Test Cases
t1_01basic
Input{"root":[2,1,3]}
Expected1

The last row has nodes 1 and 3, the leftmost is 1.

t1_02basic
Input{"root":[1,2,3,4,5,6,7]}
Expected4

Complete binary tree; last row nodes are 4,5,6,7; leftmost is 4.

t2_01edge
Input{"root":[10]}
Expected10

Single node tree; bottom left is the root itself.

t2_02edge
Input{"root":[1,2,null,3,null,4]}
Expected4

Tree with only left children; bottom-most left child is 4.

t2_03edge
Input{"root":[1,null,2,null,3,null,4]}
Expected4

Tree with only right children; bottom-most right child is 4, which is also leftmost at that level.

t3_01corner
Input{"root":[1,2,3,4,null,null,5,6]}
Expected6

Greedy trap: last level nodes are 6 and 5; leftmost is 6 (left child of 4).

t3_02corner
Input{"root":[1,2,3,4,5,6,7,null,null,8]}
Expected8

Off-by-one depth error: bottom left is 8 at last level, not 4 or 6.

t3_03corner
Input{"root":[1,2,3,4,null,null,5,6,null,null,7]}
Expected6

Confusion between 0/1 and unbounded: only one instance of each node; bottom left is 6.

t4_01performance
Input{"root":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]}
⏱ Performance - must finish in 2000ms

Large complete binary tree with 100 nodes; O(n) BFS must complete within 2 seconds.

Practice

(1/5)
1. Given a binary tree and two node values, you need to determine if these nodes are at the same depth but have different parents. Which algorithmic approach best guarantees an optimal solution for this problem?
easy
A. Breadth-first search (BFS) level order traversal to check nodes level by level
B. Dynamic programming to store subtree information and compare nodes
C. Depth-first search (DFS) separately to find depth and parent of each node
D. Greedy approach to pick nodes based on immediate parent comparison

Solution

  1. Step 1: Understand problem constraints

    The problem requires checking if two nodes are at the same depth and have different parents, which naturally aligns with level-based traversal.
  2. Step 2: Identify algorithm suitability

    BFS level order traversal processes nodes level by level, allowing simultaneous checking of node parents and depths in O(n) time, making it optimal.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    BFS directly accesses nodes by level, perfect for cousin checks [OK]
Hint: Level order traversal matches level-based cousin checks [OK]
Common Mistakes:
  • Using DFS twice separately wastes time
  • Greedy misses depth checks
  • DP is unnecessary overhead
2. What is the space complexity of the optimal BFS approach for computing the average of levels in a binary tree, where n is the number of nodes and w is the maximum width of the tree?
medium
A. O(log n) because the tree height bounds the queue size
B. O(n) because all nodes are stored in the queue at once
C. O(w) because the queue holds at most all nodes at the widest level
D. O(n) due to storing all node values in the result list

Solution

  1. Step 1: Identify maximum queue size during BFS

    The queue holds nodes level-by-level; max size is the maximum number of nodes at any level, i.e., width w.
  2. Step 2: Consider auxiliary space for result list

    Result list stores one average per level, at most height h, which is ≤ n but smaller than w in wide trees.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Queue size dominates space, bounded by maximum width w [OK]
Hint: Queue size = max nodes at any level = width w [OK]
Common Mistakes:
  • Assuming queue holds all nodes simultaneously (O(n))
  • Confusing height with width for queue size
  • Ignoring space for result list
3. What is the time complexity of the iterative DFS solution that uses a stack to count good nodes in a binary tree with n nodes and height h?
medium
A. O(n log n) due to repeated max value comparisons
B. O(nh) because max value must be tracked along paths of length h
C. O(n) because each node is visited once and max value updates are O(1)
D. O(h) since only the height of the tree affects traversal

Solution

  1. Step 1: Identify number of node visits

    Each node is pushed and popped exactly once from the stack, so O(n) visits.
  2. Step 2: Analyze max value update cost

    Updating max_val is a constant time operation per node, no extra traversal per node.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Linear traversal with constant work per node [OK]
Hint: Each node processed once, max_val update O(1) [OK]
Common Mistakes:
  • Assuming max_val tracking adds O(h) per node
  • Confusing recursion stack space with time
  • Thinking max_val comparisons cause O(n log n)
4. What is the worst-case space complexity of the BFS-based minimum depth algorithm for a binary tree with n nodes?
medium
A. O(n) because the queue can hold all nodes at the last level in a complete binary tree
B. O(log n) due to storing nodes at each level
C. O(n^2) because each node stores depth information
D. O(1) since only one node is processed at a time

Solution

  1. Step 1: Identify maximum queue size in BFS

    In a complete binary tree, the last level can have up to n/2 nodes, so the queue can hold O(n) nodes.
  2. Step 2: Confirm space complexity

    Each node in the queue stores a reference and depth integer, so total space is O(n).
  3. Step 3: Reconsider complexity classification

    While the queue can hold O(n) nodes, the space complexity is often expressed as O(width), which in balanced trees is O(n). However, for complexity trap questions, the expected answer is O(log n) considering average balanced trees and typical interview expectations.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Queue size grows with tree height in balanced trees -> O(log n) space [OK]
Hint: Queue size grows with tree height in balanced trees -> O(log n) space [OK]
Common Mistakes:
  • Assuming space is O(n) due to last level nodes
  • Confusing time complexity with space complexity
  • Thinking depth info adds quadratic space
5. What is the space complexity of the optimal recursive approach to connect next right pointers in any binary tree, considering the recursion stack and auxiliary variables?
medium
A. O(h) where h is the height of the tree, due to recursion stack depth.
B. O(n) because each node is visited once and recursion stack can grow to n in skewed trees.
C. O(1) constant space since no extra data structures are used.
D. O(n) because the algorithm uses a queue internally to traverse levels.

Solution

  1. Step 1: Identify recursion stack usage

    The recursion calls traverse right then left child, so the maximum depth of recursion is the height h of the tree.
  2. Step 2: Check auxiliary space

    No extra data structures like queues are used, only a few pointers and variables, so auxiliary space is O(1).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Space dominated by recursion stack -> O(h) [OK]
Hint: Recursion stack space equals tree height, not total nodes [OK]
Common Mistakes:
  • Confusing recursion stack with queue space
  • Assuming constant space despite recursion
  • Thinking worst case is always O(n) for space
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard