Skip to content

Commit 9c1520f

Browse files
committed
O(n) time and O(n) space using BFS deque.
1 parent f17c5c9 commit 9c1520f

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

513. Find Bottom Left Tree Value/513. Find Bottom Left Tree Value.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,19 @@
3434
# self.left = left
3535
# self.right = right
3636
class Solution:
37-
def findBottomLeftValue(self, root: TreeNode) -> int:
37+
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
38+
if not root: return None
3839
q = collections.deque()
3940
q.append(root)
40-
bottom_left = root.val
41+
result = None
4142
while q:
42-
length = len(q)
43-
for i in range(length):
43+
n = len(q)
44+
for x in range(n):
4445
node = q.popleft()
45-
if i == 0:
46-
bottom_left = node.val
46+
if x == 0:
47+
result = node.val
4748
if node.left:
4849
q.append(node.left)
4950
if node.right:
5051
q.append(node.right)
51-
return bottom_left
52+
return result

0 commit comments

Comments
 (0)