Skip to content

Commit f2f8f2a

Browse files
author
Chris Wu
committed
no message
1 parent e6e3547 commit f2f8f2a

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
_doc/*
1+
_doc/*
2+
.vscode

problems/binary-tree-pruning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ def pruneTree(self, node):
2323

2424
"""
2525
Time complexity is O(N). Because we traverse all the nodes.
26-
Space complexity is O(LogN). The tree's height is about O(LogN), so we have O(LogN) level of recursion and used up O(LogN) of stack memory
26+
Space complexity is O(N). In the worst case, the recursion will go O(N) level deep.
2727
"""

problems/path-sum-ii.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def pathSum(self, root, S):
3+
if not root: return False
4+
5+
opt = []
6+
stack = []
7+
stack.append((root, 0, []))
8+
9+
while stack:
10+
node, s, path = stack.pop()
11+
s += node.val
12+
path = path + [node.val]
13+
if not node.left and not node.right and s==S: opt.append(path)
14+
if node.left: stack.append((node.left, s, path))
15+
if node.right: stack.append((node.right, s, path))
16+
return opt
17+
"""
18+
Time complexity is O(N), because we traverse all the nodes.
19+
Space complexity is O(N^2), because in the worst case, all node could carry all the other nodes in the `path`.
20+
"""

problems/path-sum.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def hasPathSum(self, root, S):
3+
if not root: return False
4+
5+
stack = []
6+
stack.append((root, 0))
7+
8+
while stack:
9+
node, s = stack.pop()
10+
s += node.val
11+
if not node.left and not node.right and s==S: return True
12+
if node.left: stack.append((node.left, s))
13+
if node.right: stack.append((node.right, s))
14+
return False

problems/trim-a-binary-search-tree.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def trimBST(self, root, L, R):
3+
if not root: return root
4+
root.left = self.trimBST(root.left, L, R)
5+
root.right = self.trimBST(root.right, L, R)
6+
return root if L<=root.val and root.val<=R else root.left or root.right
7+
8+
"""
9+
First, trim left child and right child.
10+
Second, if the root is in range, return the root.
11+
if not, return the trimed left child.
12+
if not trimed left child, return the trimed right child.
13+
if both left and right child is None, it will return None anyway.
14+
15+
Time complexity is O(N). For we simply just traverse all the nodes.
16+
Space complexity is O(N). In the worst case, the recursion will go O(N) level deep.
17+
"""

0 commit comments

Comments
 (0)