KatInterview

513. Find Bottom Left Tree Value

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to find the value of the leftmost node in the last row of a binary tree. The "last row" refers to the deepest level of the tree. The "leftmost" node is the first one encountered at this level when traversing from left to right.

Solution

Find Bottom Left Tree Value in Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
from collections import deque

class Solution:
    def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
        # Use a deque for an efficient queue in BFS.
        # Constraints guarantee the tree is not empty, so root is not None.
        q = deque([root])
        
        # This will hold the last processed node.
        last_node = None
        
        while q:
            # Get the node from the left of the deque.
            last_node = q.popleft()
            
            # Enqueue right child first, then left child.
            # This ensures we traverse level by level, from right to left.
            # The last node processed will be the bottom-left one.
            if last_node.right:
                q.append(last_node.right)
            if last_node.left:
                q.append(last_node.left)
                
        # After the loop, last_node is the bottom-left node.
        # It will not be None due to problem constraints.
        return last_node.val

Find Bottom Left Tree Value in Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
import java.util.Queue;
import java.util.LinkedList;

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        // Queue for BFS traversal.
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        
        // This will hold the last processed node.
        TreeNode currentNode = null;
        
        while (!queue.isEmpty()) {
            // Get the node from the front of the queue.
            currentNode = queue.poll();
            
            // Enqueue right child first, then left child.
            // This ensures that we traverse level by level from right to left.
            // The last node processed will be the bottom-left one.
            if (currentNode.right != null) {
                queue.offer(currentNode.right);
            }
            if (currentNode.left != null) {
                queue.offer(currentNode.left);
            }
        }
        
        // The last node processed (currentNode) is the bottom-left node.
        // Constraints guarantee the tree has at least one node, so currentNode will not be null.
        return currentNode.val;
    }
}

Find Bottom Left Tree Value in C++

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
#include <queue>

class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        // Queue for BFS traversal.
        std::queue<TreeNode*> q;
        q.push(root);
        
        // This will hold the last processed node.
        TreeNode* currentNode = nullptr;
        
        while (!q.empty()) {
            // Get the node from the front of the queue.
            currentNode = q.front();
            q.pop();
            
            // Enqueue right child first, then left child.
            // This ensures that we traverse level by level from right to left.
            // The last node processed will be the bottom-left one.
            if (currentNode->right) {
                q.push(currentNode->right);
            }
            if (currentNode->left) {
                q.push(currentNode->left);
            }
        }
        
        // The last node processed (currentNode) is the bottom-left node.
        // Constraints guarantee the tree has at least one node, so currentNode will not be null.
        return currentNode->val;
    }
};

Find Bottom Left Tree Value in TypeScript

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function findBottomLeftValue(root: TreeNode | null): number {
    // Constraints guarantee the tree is not empty, so root is not null.
    // Using an assertion to satisfy the type checker.
    const queue: TreeNode[] = [root!];
    
    // This will hold the last processed node.
    let lastNode: TreeNode | null = null;
    
    while (queue.length > 0) {
        // Dequeue the node. The '!' asserts that queue.shift() is not undefined
        // because we just checked queue.length > 0.
        lastNode = queue.shift()!;
        
        // Enqueue right child first, then left child.
        // This ensures we traverse level by level, from right to left.
        // The last node processed will be the bottom-left one.
        if (lastNode.right) {
            queue.push(lastNode.right);
        }
        if (lastNode.left) {
            queue.push(lastNode.left);
        }
    }
    
    // After the loop, lastNode holds the bottom-left node.
    // It will not be null due to problem constraints.
    return lastNode!.val;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.