mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Update 0513.找树左下角的值.md
更改变量名称
This commit is contained in:
@ -280,31 +280,25 @@ Python:
|
|||||||
class Solution:
|
class Solution:
|
||||||
def findBottomLeftValue(self, root: TreeNode) -> int:
|
def findBottomLeftValue(self, root: TreeNode) -> int:
|
||||||
max_depth = -float("INF")
|
max_depth = -float("INF")
|
||||||
max_left_value = -float("INF")
|
leftmost_val = 0
|
||||||
|
|
||||||
def __traversal(root, left_len):
|
|
||||||
nonlocal max_depth, max_left_value
|
|
||||||
|
|
||||||
|
def __traverse(root, cur_depth):
|
||||||
|
nonlocal max_depth, leftmost_val
|
||||||
if not root.left and not root.right:
|
if not root.left and not root.right:
|
||||||
if left_len > max_depth:
|
if cur_depth > max_depth:
|
||||||
max_depth = left_len
|
max_depth = cur_depth
|
||||||
max_left_value = root.val
|
leftmost_val = root.val
|
||||||
return
|
|
||||||
|
|
||||||
if root.left:
|
if root.left:
|
||||||
left_len += 1
|
cur_depth += 1
|
||||||
__traversal(root.left, left_len)
|
__traverse(root.left, cur_depth)
|
||||||
left_len -= 1
|
cur_depth -= 1
|
||||||
|
|
||||||
if root.right:
|
if root.right:
|
||||||
left_len += 1
|
cur_depth += 1
|
||||||
__traversal(root.right, left_len)
|
__traverse(root.right, cur_depth)
|
||||||
left_len -= 1
|
cur_depth -= 1
|
||||||
return
|
|
||||||
|
|
||||||
__traversal(root, 0)
|
__traverse(root, 0)
|
||||||
|
return leftmost_val
|
||||||
return max_left_value
|
|
||||||
```
|
```
|
||||||
**迭代 - 层序遍历**
|
**迭代 - 层序遍历**
|
||||||
```python
|
```python
|
||||||
|
Reference in New Issue
Block a user