Skip to content

Commit f15efb2

Browse files
committed
no message
1 parent 4e46d7d commit f15efb2

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ Please [BUY ME A COFFEE](https://www.buymeacoffee.com/chriswu) if you want to sh
1111

1212

1313
# Leetcode Problem Lists
14-
* https://leetcode.com/list/?selectedList=535ukjh5 (Only 74 problems)
14+
I found it makes sense to solve similar problems together, so that we can recognize the problem faster when we encounter a new one. My suggestion is to skip the HARD problems when you first go through these list.
15+
1516
* https://www.programcreek.com/2013/08/leetcode-problem-classification/
1617
* https://github.com/wisdompeak/LeetCode
1718
* https://docs.google.com/spreadsheets/d/1SbpY-04Cz8EWw3A_LBUmDEXKUMO31DBjfeMoA0dlfIA/edit#gid=126913158 ([huahua](https://www.youtube.com/user/xxfflower/videos)).
19+
* https://leetcode.com/list/?selectedList=535ukjh5 (Only 74 problems)
1820

1921

2022
# Software Engineer Interview

problems/balanced-binary-tree.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,25 @@ def helper(node, depth):
2424
return -1
2525

2626
if not root: return True
27-
return helper(root, 0)!=-1
27+
return helper(root, 0)!=-1
28+
29+
30+
"""
31+
Time: O(N), since we recursively traverse each node once.
32+
Space: O(LogN), for the recursive call. O(N) if the tree is not balanced.
33+
"""
34+
class Solution(object):
35+
def isBalanced(self, root):
36+
37+
#return (if the node isBalanced, the height of the node)
38+
def helper(node):
39+
if not node: return True, -1
40+
41+
isLeftBalanced, leftHeight = helper(node.left)
42+
isRightBalanced, rightHeight = helper(node.right)
43+
44+
height = max(leftHeight, rightHeight)+1
45+
46+
return isLeftBalanced and isRightBalanced and abs(leftHeight-rightHeight)<=1, height
47+
48+
return helper(root)[0]

problems/minimum-depth-of-binary-tree.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,27 @@ def minDepth(self, root):
1818
if node.left:
1919
q.append((node.left, depth+1))
2020
if node.right:
21-
q.append((node.right, depth+1))
21+
q.append((node.right, depth+1))
22+
23+
24+
25+
"""
26+
Time: O(N)
27+
Space: O(N)
28+
29+
Standard BFS on a binary tree.
30+
"""
31+
class Solution(object):
32+
def minDepth(self, root):
33+
if not root: return 0
34+
q = collections.deque([(root, 1)])
35+
36+
while q:
37+
node, depth = q.popleft()
38+
39+
if not node.left and not node.right: return depth
40+
41+
if node.left: q.append((node.left, depth+1))
42+
if node.right: q.append((node.right, depth+1))
43+
44+
return 'ERROR'

problems/symmetric-tree.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,35 @@ def isSymmetric(self, root):
3333
# if left_node.val!=right_node.val: return False
3434
# stack.append((left_node.right, right_node.left))
3535
# stack.append((left_node.left, right_node.right))
36-
# return True
36+
# return True
37+
38+
"""
39+
Time: O(N)
40+
Space: O(N)
41+
42+
Use 2 DFS to traverse the tree at the same time.
43+
s1 will go left first.
44+
s2 will fo right first.
45+
Make sure every element popping out is the same.
46+
"""
47+
class Solution(object):
48+
def isSymmetric(self, root):
49+
s1 = [root]
50+
s2 = [root]
51+
52+
while s1 and s2:
53+
node1 = s1.pop()
54+
node2 = s2.pop()
55+
56+
if not node1 and not node2: continue
57+
if not node1 and node2: return False
58+
if node1 and not node2: return False
59+
if node1.val!=node2.val: return False
60+
61+
s1.append(node1.left)
62+
s1.append(node1.right)
63+
s2.append(node2.right)
64+
s2.append(node2.left)
65+
66+
if s1 or s2: return False #if there are something left in one of the stack
67+
return True

0 commit comments

Comments
 (0)