-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC513.py
53 lines (40 loc) · 1.11 KB
/
LC513.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def findBottomLeftValue(self, root):
"""
:type root: TreeNode
:rtype: int
"""
# 广度优先搜索
def BFS(nodes):
"""
:type nodes: [TreeNode...]
:rtype: int
"""
firstNode = nodes[0]
result = firstNode.val
nextRowNodes = []
for node in nodes:
if node.left:
nextRowNodes.append(node.left)
if node.right:
nextRowNodes.append(node.right)
if len(nextRowNodes) == 0:
return result
else:
return BFS(nextRowNodes)
result = root.val
nodes = []
if root.left:
nodes.append(root.left)
if root.right:
nodes.append(root.right)
if len(nodes) == 0:
return root.val
else:
return BFS(nodes)