Skip to content

Commit

Permalink
O(n) time and O(n) space using BFS deque.
Browse files Browse the repository at this point in the history
  • Loading branch information
149ps committed Apr 15, 2022
1 parent f17c5c9 commit 9c1520f
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@
# self.left = left
# self.right = right
class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
if not root: return None
q = collections.deque()
q.append(root)
bottom_left = root.val
result = None
while q:
length = len(q)
for i in range(length):
n = len(q)
for x in range(n):
node = q.popleft()
if i == 0:
bottom_left = node.val
if x == 0:
result = node.val
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
return bottom_left
return result

0 comments on commit 9c1520f

Please sign in to comment.