Find Bottom Left Tree Value

Updated on 27 May, 2025
Find Bottom Left Tree Value header image

Problem Statement

In this task, we are provided with the root of a binary tree. Our objective is to determine the leftmost value located in the tree's last row. This involves traversing the tree, likely assessing each level from top to bottom, and from each level, identifying the leftmost node until the final row of the tree is reached.

Examples

Example 1

Input:

root = [2,1,3]

Output:

1

Example 2

Input:

root = [1,2,3,4,null,5,6,null,null,7]

Output:

7

Constraints

  • The number of nodes in the tree is in the range [1, 104].
  • -231 <= Node.val <= 231 - 1

Approach and Intuition

To solve this problem, we can utilize a breadth-first search (BFS) strategy, typically implemented using a queue. The approach is to explore the tree level by level, starting from the root, and then proceed to all nodes of the next level from left to right. This ensures that we check all nodes at each depth level, and importantly for this task, it allows us to easily capture the leftmost node at each level as we progress.

  1. Initiate a queue and add the root of the tree to this queue.
  2. While the queue is not empty, proceed with the following steps:
    • Determine the size of the current level by checking the length of the queue.
    • For the first node of each level (the leftmost node), store its value. This can be easily achieved as it will be the first node we dequeue for that level.
    • Continue to dequeue nodes from the queue and enqueue their left and right children (if they exist) to the queue. This corresponds to adding the next level of nodes to the queue.
  3. The process repeats until all levels of the tree have been explored. The value stored from the last completed level's first node is the leftmost value of the last row of the tree.

Using this method aligns perfectly with the given constraints, as it allows us to efficiently handle the maximum constraint of up to 10,000 nodes by leveraging the level order traversal, which ensures that each node is processed exactly once.

Solutions

  • C++
  • Java
  • Python
cpp
class Solution {
public:
    int findMostLeftValue(TreeNode* root) {
        queue<TreeNode*> q;
        TreeNode* node = root;
        q.push(node);

        while (!q.empty()) {
            node = q.front();
            q.pop();
            if (node->right != nullptr) {
                q.push(node->right);
            }
            if (node->left != nullptr) {
                q.push(node->left);
            }
        }
        return node->val;
    }
};

This C++ solution focuses on determining the bottom-left value of a binary tree by utilizing a queue for level-order traversal, where the last processed node's value is the leftmost value at the deepest level of the tree.

  • Start by creating a queue, and initialise it with the root node.
  • Continuously process nodes in the queue until it is empty:
    1. Retrieve and remove the front node from the queue.
    2. First, enqueue the right child of the current node if it exists.
    3. Then, enqueue the left child.
  • When the loop exits, the last node processed represents the leftmost node at the deepest level of the tree.
  • Return the value of this node.

This approach ensures that the leftmost node at the deepest level is accessed last, capturing its value efficiently for return.

java
class Solution {
    public int getBottomLeftValue(TreeNode root) {
        Queue<TreeNode> q = new LinkedList<>();
        TreeNode node = root;
        q.add(node);

        while (!q.isEmpty()) {
            node = q.poll();
            if (node.right != null) {
                q.add(node.right);
            }
            if (node.left != null) {
                q.add(node.left);
            }
        }
        return node.val;
    }
}

The provided Java solution aims to find the bottom left value in a binary tree. This approach uses breadth-first search (BFS) for traversal, effectively employing a queue (Queue<TreeNode> q) to manage the nodes. Here's an overview of the implementation strategy:

  • Initialize a queue and add the root node to it.
  • Traverse the tree using a while loop, proceeding until the queue is empty.
  • Dequeue the front node of the queue.
  • First, enqueue the right child (if it exists), then the left child. This specific order ensures when the last node is dequeued, it will be the leftmost node since the left children are added last and will remain at the front of the queue at the final level.
  • After the loop, return the value (val) of the last dequeued node, which corresponds to the bottom left tree value.

This method ensures that the bottom-most, left-most value in the tree is efficiently determined and returned.

python
class Solution:
    def getBottomLeft(self, rootNode):
        nodeQueue = deque()
        current = rootNode
        nodeQueue.append(current)

        while nodeQueue:
            current = nodeQueue.popleft()

            if current.right:
                nodeQueue.append(current.right)

            if current.left:
                nodeQueue.append(current.left)

        return current.val

The given Python solution is designed to find the bottom left value in a binary tree using a breadth-first search (BFS) approach. The tree is traversed level by level from right to left, ensuring that the first node encountered at the last level during the traversal is the leftmost node.

  • Initialize a queue, nodeQueue, and insert the root node.
  • Use a loop to process each node in the queue:
    • Dequeue the current node.
    • Always enqueue the right child before the left child to ensure that the leftmost node at the deepest level is the last node processed.
    • Continue this until the queue is empty.
  • When the loop ends, current contains the last processed node, which is the bottom-left value of the tree.
  • Return the value of this node.

This program will correctly return the value of the bottom left node in the binary tree due to the specific order of node processing(right before left) which guarantees that the deepest and leftmost node is the last node encountered in the level-wise traversal.

Comments

No comments yet.