Skip to content

Commit 2acab52

Browse files
committed
no message
1 parent f15efb2 commit 2acab52

4 files changed

+105
-13
lines changed

problems/binary-search-tree-iterator.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
# Definition for a binary tree node.
2-
# class TreeNode(object):
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.left = None
6-
# self.right = None
7-
81
class BSTIterator(object):
92

103
def __init__(self, root):
@@ -27,10 +20,45 @@ def next(self):
2720

2821
def hasNext(self):
2922
return len(self.stack)!=0
23+
24+
"""
25+
Time: init() O(1). next() O(LogN). hasNext() O(1).
26+
Space: O(N)
27+
"""
28+
class BSTIterator(object):
29+
30+
def __init__(self, root):
31+
self.node = root
32+
self.stack = []
33+
34+
def next(self):
35+
while self.node:
36+
self.stack.append(self.node)
37+
self.node = self.node.left
38+
39+
node = self.stack.pop()
40+
self.node = node.right
41+
return node.val
3042

43+
def hasNext(self):
44+
return self.stack or self.node
3145

3246

33-
# Your BSTIterator object will be instantiated and called as such:
34-
# obj = BSTIterator(root)
35-
# param_1 = obj.next()
36-
# param_2 = obj.hasNext()
47+
"""
48+
FYI. Template for in-order traverse.
49+
"""
50+
def inOrderTraverse(root):
51+
stack = []
52+
node = root
53+
54+
while node or stack:
55+
while node:
56+
stack.append(node)
57+
node = node.left
58+
59+
node = stack.pop()
60+
61+
#do something
62+
print node.val
63+
64+
node = node.right

problems/binary-tree-right-side-view.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,28 @@ def rightSideView(self, root):
2828
view.append(node.val)
2929
queue.append((node.right, level+1))
3030
queue.append((node.left, level+1))
31-
return view
31+
return view
32+
33+
"""
34+
Time: O(N).
35+
Space: O(N).
36+
37+
Similar solution using DFS.
38+
"""
39+
class Solution(object):
40+
def rightSideView(self, root):
41+
if not root: return []
42+
43+
ans = []
44+
currLevel = -1
45+
stack = [(root, 0)]
46+
47+
while stack:
48+
node, level = stack.pop()
49+
if level>currLevel:
50+
ans.append(node.val)
51+
currLevel = level
52+
if node.left: stack.append((node.left, level+1))
53+
if node.right: stack.append((node.right, level+1))
54+
55+
return ans
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Time: O(LogN). O(N) if the tree is unbalanced.
3+
Space: O(1)
4+
5+
If both p and q are both on the left subtree, then search the left subtree.
6+
If both p and q are both on the right subtree, then search the right subtree.
7+
Else the current must be the ans.
8+
"""
9+
class Solution(object):
10+
def lowestCommonAncestor(self, root, p, q):
11+
node = root
12+
13+
while node:
14+
if node.val>q.val and node.val>p.val:
15+
node = node.left
16+
elif node.val<q.val and node.val<p.val:
17+
node = node.right
18+
else:
19+
return node

problems/lowest-common-ancestor-of-a-binary-tree.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,25 @@ def find_lowest_common(a1, a2):
129129
find_ancestors() is O(LogN).
130130
find_lowest_common is O(LogN).
131131
Space complexity is O(N), since `genealogy` may carry all the nodes.
132-
"""
132+
"""
133+
134+
135+
# 2021/9/25
136+
class Solution(object):
137+
def lowestCommonAncestor(self, root, p, q):
138+
def __init__(self):
139+
self.ans = None
140+
141+
def helper(node):
142+
143+
if not node: return False
144+
mid = node==q or node==p
145+
left = helper(node.left)
146+
right = helper(node.right)
147+
148+
if (mid and left) or (mid and right) or (left and right): self.ans = node
149+
150+
return mid or left or right
151+
152+
helper(root)
153+
return self.ans

0 commit comments

Comments
 (0)