Skip to content

Commit 0ec60f6

Browse files
author
chris
committed
no message
1 parent b443fd3 commit 0ec60f6

4 files changed

+127
-12
lines changed

binary-tree-right-side-view.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
perform BFS to the tree:
3+
we traverse every node in the level then go to the next level
4+
every level we traverse from right to left
5+
if it is a level we haven't been through before
6+
we add the node's val to the output
7+
8+
time efficiency is O(N)
9+
because we basically go through all the nodes
10+
N is the total nodes count
11+
12+
space efficiency is O(N)
13+
since we my store the whole level of the tree in the queue
14+
the last level is 0.5N
15+
N is the total nodes count
16+
"""
17+
from collections import deque
18+
class Solution(object):
19+
def rightSideView(self, root):
20+
queue = deque([(root, 0)])
21+
max_level = -1
22+
view = []
23+
while queue:
24+
node, level = queue.popleft()
25+
if node==None: continue
26+
if level>max_level:
27+
max_level = level
28+
view.append(node.val)
29+
queue.append((node.right, level+1))
30+
queue.append((node.left, level+1))
31+
return view

maximum-subarray.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
#https://leetcode.com/problems/maximum-subarray/
2+
"""
3+
[0]
4+
if we know the max value of the subarray that ends at i index is Mi
5+
what is the max value of the subarray that ends at i+1 index?
6+
its either nums[i+1] or nums[i+1]+Mi
7+
so code below, maxCurrent[i] stores the max value of subarray that ends at i
8+
9+
[1]
10+
the max value of the subarray that ends at 0, has to be nums[0].
11+
12+
[2]
13+
the max value of subarray must ends in one of the index of nums
14+
so we get the max(maxCurrent).
15+
"""
216
class Solution(object):
317
def maxSubArray(self, nums):
4-
maxCurrent = []
5-
for i in range(0, len(nums)):
6-
n = nums[i]
7-
if (i==0):
8-
maxCurrent.append(n)
9-
else:
10-
if (n>n+maxCurrent[i-1]):
11-
maxCurrent.append(n)
12-
else:
13-
maxCurrent.append(n+maxCurrent[i-1])
14-
return max(maxCurrent)
18+
if nums==None or len(nums)==0: return None
19+
maxCurrent = [nums[0]] #[1]
20+
for i in xrange(1, len(nums)):
21+
maxCurrent.append(max(nums[i], nums[i]+maxCurrent[-1])) #[0]
22+
return max(maxCurrent) #[2]

search-in-rotated-sorted-array.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,6 @@ def search(self, nums, target):
6969
#l~m is not sorted
7070
#check l~m-1
7171
r = m-1
72-
continue
72+
continue
73+
74+
return -1

validate-binary-search-tree.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
"""
2+
1.
3+
for recursive/iterative solution
4+
if you are a left node
5+
your max will be your parent's value
6+
your min will be yout parent's min, because your parent might be someone's right child.
7+
8+
if you are a right node
9+
your min will be your parent's value
10+
your max will be your parent's max, because your parent might be someone's left child.
11+
12+
root have no max and min
13+
14+
2.
15+
for in-order traversal solution
16+
if you use in-order to traverse a BST
17+
the value you list out will be in order(sorted)
18+
so each element would be greater than the last one
19+
20+
in-order traverse:
21+
left child -> current node -> right child
22+
23+
normaly I will recursivly do the in-order traversing
24+
but iterative is easier to track the last element'value
25+
26+
3. I personally like in-order traversal solution
27+
"""
28+
class Solution(object):
29+
#recursive
30+
def isValidBST(self, root):
31+
def helper(node, min_val, max_val):
32+
if node==None:
33+
return True
34+
if node.val<=min_val or node.val>=max_val:
35+
return False
36+
37+
left_valid = helper(node.left, min_val, node.val)
38+
if not left_valid: return False
39+
right_valid = helper(node.right, node.val, max_val)
40+
if not right_valid: return False
41+
42+
return True
43+
return helper(root, float('-inf'), float('inf'))
44+
45+
#iterative
46+
def isValidBST(self, root):
47+
if root==None: return True
48+
stack = [(root, float('-inf'), float('inf'))]
49+
while stack:
50+
node, min_val, max_val = stack.pop()
51+
if node.val<=min_val or node.val>=max_val:
52+
return False
53+
if node.left:
54+
stack.append((node.left, min_val, node.val))
55+
if node.right:
56+
stack.append((node.right, node.val, max_val))
57+
return True
58+
59+
#in-order traversal
60+
def isValidBST(self, root):
61+
stack = []
62+
last_val = float('-inf')
63+
while root or stack:
64+
while root:
65+
stack.append(root)
66+
root = root.left
67+
root = stack.pop()
68+
69+
if root.val<=last_val:
70+
return False
71+
72+
last_val = root.val
73+
root = root.right #if root.right: root = root.right
74+
return True

0 commit comments

Comments
 (0)