Skip to content

Commit 8c53c48

Browse files
committed
Revert "更新 513.找树左下角的值 python 部分代码"
This reverts commit 2f412e5.
1 parent 2f412e5 commit 8c53c48

File tree

1 file changed

+12
-49
lines changed

1 file changed

+12
-49
lines changed

problems/0513.找树左下角的值.md

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,8 @@ class Solution {
274274

275275

276276
Python:
277-
278-
**递归法 - 回溯**
279277
```python
278+
//递归法
280279
# Definition for a binary tree node.
281280
# class TreeNode:
282281
# def __init__(self, val=0, left=None, right=None):
@@ -285,53 +284,17 @@ Python:
285284
# self.right = right
286285
class Solution:
287286
def findBottomLeftValue(self, root: TreeNode) -> int:
288-
max_depth = -float("INF")
289-
max_left_value = -float("INF")
290-
291-
def __traversal(root, left_len):
292-
nonlocal max_depth, max_left_value
293-
294-
if not root.left and not root.right:
295-
if left_len > max_depth:
296-
max_depth = left_len
297-
max_left_value = root.val
298-
return
299-
300-
if root.left:
301-
left_len += 1
302-
__traversal(root.left, left_len)
303-
left_len -= 1
304-
305-
if root.right:
306-
left_len += 1
307-
__traversal(root.right, left_len)
308-
left_len -= 1
309-
return
310-
311-
__traversal(root, 0)
312-
313-
return max_left_value
314-
```
315-
316-
**迭代法 - 层序遍历**
317-
```python
318-
class Solution:
319-
def findBottomLeftValue(self, root: TreeNode) -> int:
320-
queue = deque()
321-
if root:
322-
queue.append(root)
323-
result = 0
324-
while queue:
325-
q_len = len(queue)
326-
for i in range(q_len):
327-
if i == 0:
328-
result = queue[i].val
329-
cur = queue.popleft()
330-
if cur.left:
331-
queue.append(cur.left)
332-
if cur.right:
333-
queue.append(cur.right)
334-
return result
287+
depth=0
288+
self.res=[]
289+
def level(root,depth):
290+
if not root:return
291+
if depth==len(self.res):
292+
self.res.append([])
293+
self.res[depth].append(root.val)
294+
level(root.left,depth+1)
295+
level(root.right,depth+1)
296+
level(root,depth)
297+
return self.res[-1][0]
335298
```
336299
Go:
337300

0 commit comments

Comments
 (0)