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:
[1, 104]
.-231 <= Node.val <= 231 - 1
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the bottom-left value in a tree, a brute-force approach involves exploring every single node. We essentially want to visit all the nodes and keep track of the leftmost node at the deepest level we've seen so far.
Here's how the algorithm would work step-by-step:
def find_bottom_left_tree_value(root):
deepest_level = -1
leftmost_value_at_deepest_level = 0
def depth_first_search(node, current_depth):
nonlocal deepest_level, leftmost_value_at_deepest_level
if node is None:
return
# If we've found a new deepest level
if current_depth > deepest_level:
deepest_level = current_depth
# Update the leftmost value at the deepest level
leftmost_value_at_deepest_level = node.val
# Explore left subtree first to find leftmost node
depth_first_search(node.left, current_depth + 1)
depth_first_search(node.right, current_depth + 1)
depth_first_search(root, 0)
return leftmost_value_at_deepest_level
To find the bottom-left value in a tree, we can use a breadth-first search approach, visiting nodes level by level. The leftmost node on the very last level we visit is the answer.
Here's how the algorithm would work step-by-step:
def findBottomLeftValue(root):
queue = [root]
bottomLeftValue = 0
while queue:
levelLength = len(queue)
# Set to first value in level, this is most left one
bottomLeftValue = queue[0].val
for i in range(levelLength):
currentNode = queue.pop(0)
if currentNode.left:
queue.append(currentNode.left)
if currentNode.right:
queue.append(currentNode.right)
# The last most left value is the result
return bottomLeftValue
Case | How to Handle |
---|---|
Null root node | Return null or a predefined value (e.g., -1) since there is no tree. |
Tree with only one node (root) | Return the value of the root node immediately as it is both the leftmost and bottommost. |
Complete binary tree of significant depth | BFS implementation guarantees the bottom level is reached efficiently; memory usage should be considered. |
Highly unbalanced tree skewed to the right | BFS still functions correctly but might not be as efficient as for a balanced tree; consider if DFS could be more appropriate based on constraints. |
Highly unbalanced tree skewed to the left | BFS will correctly identify the bottom left value. |
Tree with duplicate node values | Duplicate node values do not affect the correctness of BFS or DFS as long as the leftmost node on the bottom level is correctly identified. |
Tree with negative or zero node values | These values are valid node values and do not require special handling; the algorithm will process them as usual. |
Integer overflow during node value comparison or storage | Ensure the data type used for node values and any intermediate calculations is large enough to prevent overflow; use long if necessary. |