Skip to content

Commit 82e5f93

Browse files
author
Chris Wu
committed
no message
1 parent cebd5c6 commit 82e5f93

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

problems/diameter-of-binary-tree.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ def getMaxDepth(node):
4545
From an unknown node, that its max_depth_from_left (`l`) + max_depth_from_right (`r`) is the biggest.
4646
The node that generate this number could be from any node, so we iterate through every node to update `ans`.
4747
In other words, to find the answer, we need to check every node, if the max diameter pass through here.
48+
49+
Time complexity is O(N), where N is the number of nodes.
50+
Space complexity is O(LogN), since we might got to LogN level on recursion.
4851
"""
4952

5053
# from collections import defaultdict

problems/sum-root-to-leaf-numbers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ def sumNumbers(self, root):
88
while stack:
99
node, total = stack.pop()
1010
total += str(node.val)
11-
if not node.left and not node.right:
11+
if not node.left and not node.right: #is_leaf
1212
ans += int(total)
1313
continue
1414
if node.left:
1515
stack.append((node.left, total))
1616
if node.right:
1717
stack.append((node.right, total))
1818
return ans
19+
20+
"""
21+
Time complexity: O(N), since we use DFS to traverse each node once.
22+
Space complexity: O(NLogN), since each node (at the very bottom), may carry all the digit from its ancestor, LogN.
23+
"""

0 commit comments

Comments
 (0)