Skip to content

Commit 6c6b1c0

Browse files
committed
no message
1 parent a20b61a commit 6c6b1c0

File tree

3 files changed

+112
-19
lines changed

3 files changed

+112
-19
lines changed

problems/delete-node-in-a-bst.py

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
"""
2+
Find the parant and the node to be deleted. [0]
3+
Deleting the node means replacing its reference by something else. [1]
4+
5+
For the node to be deleted, if it only has no child, just remove it from the parent. Return None. [2]
6+
7+
If it has one child, return the child. So its parent will directly connect to its child. [3]
8+
9+
If it has both child. Update the node's val to the minimum value in the right subtree. Remove the minimum value node in the right subtree. [4]
10+
This is equivalent to replacing the node by the minimum value node in the right subtree.
11+
Another option is to replace the node by the maximum value node in the left subtree.
12+
13+
Find the minimum value in the left subtree is easy. The leftest node value in the tree is the smallest. [5]
14+
15+
Time Complexity: O(LogN). O(LogN) for finding the node to be deleted.
16+
The recursive call in `remove()` will be apply to a much smaller subtree. And much smaller subtree...
17+
So can be ignored.
18+
Space complexity is O(LogN). Because the recursive call will at most be called LogN times.
19+
N is the number of nodes. And LogN can be consider the height of the tree.
20+
"""
121
class Solution(object):
222
def deleteNode(self, root, key):
323
def find_min(root):
@@ -39,23 +59,41 @@ def remove(node):
3959

4060
return root
4161

42-
"""
43-
Find the parant and the node to be deleted. [0]
44-
Deleting the node means replacing its reference by something else. [1]
4562

46-
For the node to be deleted, if it only has no child, just remove it from the parent. Return None. [2]
4763

48-
If it has one child, return the child. So its parent will directly connect to its child. [3]
4964

50-
If it has both child. Update the node's val to the minimum value in the right subtree. Remove the minimum value node in the right subtree. [4]
51-
This is equivalent to replacing the node by the minimum value node in the right subtree.
52-
Another option is to replace the node by the maximum value node in the left subtree.
65+
class Solution(object):
66+
def deleteNode(self, node, key):
67+
if not node:
68+
return node
69+
elif key<node.val:
70+
node.left = self.deleteNode(node.left, key)
71+
return node
72+
elif key>node.val:
73+
node.right = self.deleteNode(node.right, key)
74+
return node
75+
elif key==node.val:
76+
if not node.left and not node.right:
77+
return None
78+
elif not node.left:
79+
return node.right
80+
elif not node.right:
81+
return node.left
82+
else:
83+
node.val = self.findMin(node.right) #min value in the right subtree
84+
node.right = self.deleteNode(node.right, node.val)
85+
return node
86+
87+
def findMin(self, root):
88+
ans = float('inf')
89+
stack = [root]
90+
91+
while stack:
92+
node = stack.pop()
93+
ans = min(ans, node.val)
94+
if node.left: stack.append(node.left)
95+
if node.right: stack.append(node.right)
96+
return ans
97+
5398

54-
Find the minimum value in the left subtree is easy. The leftest node value in the tree is the smallest. [5]
5599

56-
Time Complexity: O(LogN). O(LogN) for finding the node to be deleted.
57-
The recursive call in `remove()` will be apply to a much smaller subtree. And much smaller subtree...
58-
So can be ignored.
59-
Space complexity is O(LogN). Because the recursive call will at most be called LogN times.
60-
N is the number of nodes. And LogN can be consider the height of the tree.
61-
"""

problems/minimum-absolute-difference-in-bst.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,47 @@ def traverse(node):
4343
"""
4444
Time complexity: O(N)
4545
Space complexity: O(N)
46-
"""
46+
"""
47+
48+
49+
"""
50+
Inorder traverse the BST and update the diff.
51+
Time complexity: O(N)
52+
Space complexity: O(N)
53+
"""
54+
class Solution(object):
55+
def getMinimumDifference(self, root):
56+
ans = float('inf')
57+
58+
stack = []
59+
curr = root
60+
prevVal = float('-inf')
61+
62+
while stack or curr:
63+
while curr:
64+
stack.append(curr)
65+
curr = curr.left
66+
67+
curr = stack.pop()
68+
ans = min(ans, curr.val-prevVal)
69+
prevVal = curr.val
70+
71+
curr = curr.right
72+
return ans
73+
74+
75+
def inorderTraversal(self, root):
76+
stack = []
77+
curr = root
78+
79+
while stack or curr:
80+
while curr:
81+
stack.append(curr)
82+
curr = curr.left
83+
84+
curr = stack.pop()
85+
86+
# do something
87+
print curr.val
88+
89+
curr = curr.right

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
"""
2+
Time complexity: O(LogN)
3+
Space complexity: O(1)
4+
"""
15
class Solution(object):
26
def searchBST(self, root, val):
37
node = root
@@ -12,8 +16,16 @@ def searchBST(self, root, val):
1216

1317
return None
1418

15-
1619
"""
1720
Time complexity: O(LogN)
18-
Space complexity: O(1)
19-
"""
21+
Space complexity: O(LogN)
22+
"""
23+
class Solution(object):
24+
def searchBST(self, node, val):
25+
if not node: return None
26+
if node.val==val:
27+
return node
28+
elif node.val<val:
29+
return self.searchBST(node.right, val)
30+
elif node.val>val:
31+
return self.searchBST(node.left, val)

0 commit comments

Comments
 (0)