Skip to content

Commit b0e6112

Browse files
authored
Create 230_kthSmallestElementBST.py
1 parent 5aa9758 commit b0e6112

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

LeetCode/230_kthSmallestElementBST.py

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
"""
2+
Level: MEDIUM
3+
https://leetcode.com/problems/kth-smallest-element-in-a-bst/
4+
5+
57 ms with test data
6+
7+
8+
Runtime
9+
57 ms
10+
Beats
11+
81.22%
12+
Memory
13+
18 MB
14+
Beats
15+
45.99%
16+
"""
17+
18+
# Definition for a binary tree node.
19+
# class TreeNode:
20+
# def __init__(self, val=0, left=None, right=None):
21+
# self.val = val
22+
# self.left = left
23+
# self.right = right
24+
class Solution:
25+
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
26+
def countup(node):
27+
if node is None:
28+
return 0
29+
else:
30+
return 1 + countup(node.left) + countup(node.right)
31+
32+
def goback(node):
33+
if node is None:
34+
return -1
35+
nodecount = countup(node.left) + 1
36+
if nodecount == k:
37+
return node.val
38+
elif nodecount < k and node.right is not None:
39+
return self.kthSmallest(node.right,k-nodecount)
40+
elif nodecount > k and node.left is not None:
41+
return goback(node.left)
42+
else:
43+
return -1
44+
45+
return goback(root)
46+
47+
48+
"""
49+
36ms with test data
50+
51+
Runtime
52+
47 ms
53+
Beats
54+
96.77%
55+
Memory
56+
18.1 MB
57+
Beats
58+
16.44%
59+
"""
60+
# Definition for a binary tree node.
61+
# class TreeNode:
62+
# def __init__(self, val=0, left=None, right=None):
63+
# self.val = val
64+
# self.left = left
65+
# self.right = right
66+
class Solution:
67+
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
68+
def goback(node,count):
69+
if node is None:
70+
return (-1,0)
71+
leftans, leftcount = goback(node.left,count)
72+
if leftcount == k:
73+
return (leftans,leftcount)
74+
nodecount = leftcount + count + 1
75+
#print(f"node {node.val}, count {nodecount}, parentcount {count}")
76+
if nodecount == k:
77+
return (node.val,nodecount)
78+
rightans,rightcount = goback(node.right,nodecount)
79+
if rightcount == k:
80+
return (rightans,rightcount)
81+
return (rightans,leftcount+rightcount+1)
82+
83+
ans,count = goback(root,0)
84+
return ans
85+
86+
87+
"""
88+
In order traversal with stack
89+
class Solution:
90+
def kthSmallest(self, root: TreeNode, k: int) -> int:
91+
stack = []
92+
res = []
93+
while stack or root:
94+
while root:
95+
stack.append(root)
96+
root = root.left
97+
root = stack.pop()
98+
res.append(root)
99+
root = root.right
100+
return res[k-1].val
101+
102+
recursive in order traversal with early stopping
103+
def kthSmallest_dfs_early_stopping(self, root, k):
104+
res = []
105+
def _inorder(node):
106+
if not node: return
107+
_inorder(node.left)
108+
if len(res) == k:
109+
return
110+
res.append(node.val)
111+
_inorder(node.right)
112+
_inorder(root)
113+
return res[-1]
114+
115+
116+
Faster 98.65% Memory 99.79% || TC : O(N) || SC: O(1) MORRIS TRAVERSAL
117+
118+
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
119+
120+
pos = 0
121+
ans = 0
122+
123+
current = root
124+
125+
while current and pos < k:
126+
127+
if not current.left:
128+
pos += 1
129+
ans = current.val
130+
current = current.right
131+
132+
else:
133+
pre = current.left
134+
while pre.right:
135+
pre = pre.right
136+
137+
pre.right = current
138+
left = current.left
139+
current.left = None
140+
current = left
141+
142+
return ans
143+
"""
144+

0 commit comments

Comments
 (0)