-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path12_binary_search_tree_kth_smallest_element.py
69 lines (51 loc) · 1.41 KB
/
12_binary_search_tree_kth_smallest_element.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class BinarySearchTree:
def __init__(self):
self.root = None
def kthSmallest(self, root: 'TreeNode', k: int) -> int:
n = 0
stack = []
cur = root
while cur or stack:
while cur:
stack.append(cur)
cur = cur.left
cur = stack.pop()
n += 1
if n == k:
return cur.val
cur = cur.right
# Helper function to build a BST from a list
def buildBST(nums):
if not nums:
return None
root = TreeNode(nums[0])
for num in nums[1:]:
if num is not None:
root = insert(root, num)
return root
# Helper function to insert a value into a BST
def insert(root, val):
if not root:
return TreeNode(val)
if val < root.val:
root.left = insert(root.left, val)
else:
root.right = insert(root.right, val)
return root
if __name__ == "__main__":
solution = BinarySearchTree()
nums_1 = [3,1,4,None,2]
k1 = 1
root1 = buildBST(nums_1)
result1 = solution.kthSmallest(root1, k1)
print(result1)
nums_2 = [5,3,6,2,4,None,None,1]
k2 = 3
root2 = buildBST(nums_2)
result2 = solution.kthSmallest(root2, 3)
print(result2)