513. Find Bottom Left Tree Value LeetCode Solution

In this guide, you will get 513. Find Bottom Left Tree Value LeetCode Solution with the best time and space complexity. The solution to Find Bottom Left Tree Value problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Bottom Left Tree Value solution in C++
  4. Find Bottom Left Tree Value solution in Java
  5. Find Bottom Left Tree Value solution in Python
  6. Additional Resources
513. Find Bottom Left Tree Value LeetCode Solution image

Problem Statement of Find Bottom Left Tree Value

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

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

513. Find Bottom Left Tree Value LeetCode Solution in C++

class Solution {
 public:
  int findBottomLeftValue(TreeNode* root) {
    queue<TreeNode*> q{{root}};
    TreeNode* node = nullptr;

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

    return node->val;
  }
};
/* code provided by PROGIEZ */

513. Find Bottom Left Tree Value LeetCode Solution in Java

class Solution {
  public int findBottomLeftValue(TreeNode root) {
    Queue<TreeNode> q = new ArrayDeque<>(List.of(root));
    TreeNode node = null;

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

    return node.val;
  }
}
// code provided by PROGIEZ

513. Find Bottom Left Tree Value LeetCode Solution in Python

class Solution:
  def findBottomLeftValue(self, root: TreeNode | None) -> int:
    q = collections.deque([root])

    while q:
      root = q.popleft()
      if root.right:
        q.append(root.right)
      if root.left:
        q.append(root.left)

    return root.val
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.