mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 02:53:31 +08:00
Revert "更新 513.找树左下角的值 python 部分代码"
This reverts commit 2f412e5c7d3b41b66946d92e699330cb7e5e5036.
This commit is contained in:
@ -274,9 +274,8 @@ class Solution {
|
||||
|
||||
|
||||
Python:
|
||||
|
||||
**递归法 - 回溯**
|
||||
```python
|
||||
//递归法
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
@ -285,53 +284,17 @@ Python:
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def findBottomLeftValue(self, root: TreeNode) -> int:
|
||||
max_depth = -float("INF")
|
||||
max_left_value = -float("INF")
|
||||
|
||||
def __traversal(root, left_len):
|
||||
nonlocal max_depth, max_left_value
|
||||
|
||||
if not root.left and not root.right:
|
||||
if left_len > max_depth:
|
||||
max_depth = left_len
|
||||
max_left_value = root.val
|
||||
return
|
||||
|
||||
if root.left:
|
||||
left_len += 1
|
||||
__traversal(root.left, left_len)
|
||||
left_len -= 1
|
||||
|
||||
if root.right:
|
||||
left_len += 1
|
||||
__traversal(root.right, left_len)
|
||||
left_len -= 1
|
||||
return
|
||||
|
||||
__traversal(root, 0)
|
||||
|
||||
return max_left_value
|
||||
```
|
||||
|
||||
**迭代法 - 层序遍历**
|
||||
```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
|
||||
depth=0
|
||||
self.res=[]
|
||||
def level(root,depth):
|
||||
if not root:return
|
||||
if depth==len(self.res):
|
||||
self.res.append([])
|
||||
self.res[depth].append(root.val)
|
||||
level(root.left,depth+1)
|
||||
level(root.right,depth+1)
|
||||
level(root,depth)
|
||||
return self.res[-1][0]
|
||||
```
|
||||
Go:
|
||||
|
||||
|
Reference in New Issue
Block a user