mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Update 0513.找树左下角的值.md
This commit is contained in:
@ -271,52 +271,80 @@ class Solution {
|
|||||||
|
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
(版本一)递归法 + 回溯
|
||||||
递归:
|
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def findBottomLeftValue(self, root: TreeNode) -> int:
|
def findBottomLeftValue(self, root: TreeNode) -> int:
|
||||||
max_depth = -float("INF")
|
self.max_depth = float('-inf')
|
||||||
leftmost_val = 0
|
self.result = None
|
||||||
|
self.traversal(root, 0)
|
||||||
|
return self.result
|
||||||
|
|
||||||
def __traverse(root, cur_depth):
|
def traversal(self, node, depth):
|
||||||
nonlocal max_depth, leftmost_val
|
if not node.left and not node.right:
|
||||||
if not root.left and not root.right:
|
if depth > self.max_depth:
|
||||||
if cur_depth > max_depth:
|
self.max_depth = depth
|
||||||
max_depth = cur_depth
|
self.result = node.val
|
||||||
leftmost_val = root.val
|
return
|
||||||
if root.left:
|
|
||||||
cur_depth += 1
|
if node.left:
|
||||||
__traverse(root.left, cur_depth)
|
depth += 1
|
||||||
cur_depth -= 1
|
self.traversal(node.left, depth)
|
||||||
if root.right:
|
depth -= 1
|
||||||
cur_depth += 1
|
if node.right:
|
||||||
__traverse(root.right, cur_depth)
|
depth += 1
|
||||||
cur_depth -= 1
|
self.traversal(node.right, depth)
|
||||||
|
depth -= 1
|
||||||
|
|
||||||
__traverse(root, 0)
|
|
||||||
return leftmost_val
|
|
||||||
```
|
```
|
||||||
|
|
||||||
迭代 - 层序遍历:
|
(版本二)递归法+精简
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def findBottomLeftValue(self, root: TreeNode) -> int:
|
def findBottomLeftValue(self, root: TreeNode) -> int:
|
||||||
queue = deque()
|
self.max_depth = float('-inf')
|
||||||
if root:
|
self.result = None
|
||||||
queue.append(root)
|
self.traversal(root, 0)
|
||||||
result = 0
|
return self.result
|
||||||
|
|
||||||
|
def traversal(self, node, depth):
|
||||||
|
if not node.left and not node.right:
|
||||||
|
if depth > self.max_depth:
|
||||||
|
self.max_depth = depth
|
||||||
|
self.result = node.val
|
||||||
|
return
|
||||||
|
|
||||||
|
if node.left:
|
||||||
|
self.traversal(node.left, depth+1)
|
||||||
|
if node.right:
|
||||||
|
self.traversal(node.right, depth+1)
|
||||||
|
```
|
||||||
|
|
||||||
|
(版本三) 迭代法
|
||||||
|
```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:
|
||||||
|
queue = deque([root])
|
||||||
while queue:
|
while queue:
|
||||||
q_len = len(queue)
|
size = len(queue)
|
||||||
for i in range(q_len):
|
leftmost = queue[0].val
|
||||||
if i == 0:
|
for i in range(size):
|
||||||
result = queue[i].val
|
node = queue.popleft()
|
||||||
cur = queue.popleft()
|
if node.left:
|
||||||
if cur.left:
|
queue.append(node.left)
|
||||||
queue.append(cur.left)
|
if node.right:
|
||||||
if cur.right:
|
queue.append(node.right)
|
||||||
queue.append(cur.right)
|
if not queue:
|
||||||
return result
|
return leftmost
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
Reference in New Issue
Block a user