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