Skip to content

Commit 53798c9

Browse files
committed
Added Q513
1 parent 4f16aa4 commit 53798c9

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
id: find-bottom-left-tree-value
3+
title: Find Bottom Left Tree Value
4+
sidebar_label: 0513-Find-Bottom-Left-Tree-Value
5+
tags:
6+
- Tree
7+
- Depth-First Search
8+
- Breadth-First Search
9+
- Binary Tree
10+
description: "Given the root of a binary tree, return the leftmost value in the last row of the tree."
11+
---
12+
13+
## Problem
14+
15+
Given the root of a binary tree, return the leftmost value in the last row of the tree.
16+
17+
### Examples
18+
19+
**Example 1:**
20+
21+
**Input:** `root = [2,1,3]`
22+
**Output:** `1`
23+
24+
**Example 2:**
25+
26+
**Input:** `root = [1,2,3,4,null,5,6,null,null,7]`
27+
**Output:** `7`
28+
29+
### Constraints
30+
31+
- The number of nodes in the tree is in the range `[1, 10^4]`.
32+
- `-2^31 <= Node.val <= 2^31 - 1`
33+
34+
---
35+
36+
## Approach
37+
38+
To find the leftmost value in the last row of the binary tree, we can use either Breadth-First Search (BFS) or Depth-First Search (DFS). The BFS approach involves traversing the tree level by level from left to right, ensuring that the first node encountered at the last level is the leftmost node. The DFS approach involves keeping track of the depth and updating the leftmost value when a deeper node is found.
39+
40+
### Steps for BFS:
41+
42+
1. Initialize a queue and enqueue the root node.
43+
2. While the queue is not empty, process each level:
44+
- Keep track of the first node at each level.
45+
- Enqueue all child nodes of the current level nodes.
46+
3. The leftmost value of the last processed level is the desired result.
47+
48+
### Steps for DFS:
49+
50+
1. Initialize variables to keep track of the maximum depth and the leftmost value.
51+
2. Perform a DFS traversal, updating the leftmost value whenever a deeper node is found.
52+
53+
### Solution
54+
55+
#### Java Solution
56+
57+
```java
58+
import java.util.LinkedList;
59+
import java.util.Queue;
60+
61+
public class TreeNode {
62+
int val;
63+
TreeNode left;
64+
TreeNode right;
65+
TreeNode(int x) { val = x; }
66+
}
67+
68+
class Solution {
69+
public int findBottomLeftValue(TreeNode root) {
70+
Queue<TreeNode> queue = new LinkedList<>();
71+
queue.add(root);
72+
int bottomLeftValue = root.val;
73+
74+
while (!queue.isEmpty()) {
75+
TreeNode node = queue.poll();
76+
if (node.right != null) queue.add(node.right);
77+
if (node.left != null) queue.add(node.left);
78+
bottomLeftValue = node.val;
79+
}
80+
81+
return bottomLeftValue;
82+
}
83+
}
84+
```
85+
### C++ Solution
86+
87+
```cpp
88+
#include <queue>
89+
using namespace std;
90+
91+
struct TreeNode {
92+
int val;
93+
TreeNode *left;
94+
TreeNode *right;
95+
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
96+
};
97+
98+
class Solution {
99+
public:
100+
int findBottomLeftValue(TreeNode* root) {
101+
queue<TreeNode*> q;
102+
q.push(root);
103+
int bottomLeftValue = root->val;
104+
105+
while (!q.empty()) {
106+
TreeNode* node = q.front();
107+
q.pop();
108+
if (node->right) q.push(node->right);
109+
if (node->left) q.push(node->left);
110+
bottomLeftValue = node->val;
111+
}
112+
113+
return bottomLeftValue;
114+
}
115+
};
116+
```
117+
### Python
118+
119+
```python
120+
from collections import deque
121+
from typing import Optional
122+
123+
# Definition for a binary tree node.
124+
class TreeNode:
125+
def __init__(self, val=0, left=None, right=None):
126+
self.val = val
127+
self.left = left
128+
self.right = right
129+
130+
class Solution:
131+
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
132+
queue = deque([root])
133+
bottom_left_value = root.val
134+
135+
while queue:
136+
node = queue.popleft()
137+
if node.right:
138+
queue.append(node.right)
139+
if node.left:
140+
queue.append(node.left)
141+
bottom_left_value = node.val
142+
143+
return bottom_left_value
144+
```
145+
### Complexity Analysis
146+
**Time Complexity:** O(n)
147+
>Reason: Each node is visited once during the traversal.
148+
149+
**Space Complexity:** O(n)
150+
>Reason: In the worst case, the space required for the queue or recursion stack is proportional to the number of nodes.
151+
152+
### References
153+
LeetCode Problem: Find Bottom Left Tree Value
154+

0 commit comments

Comments
 (0)