Skip to content

Commit f770374

Browse files
committed
solve: kth-smallest-element-in-a-bst
1 parent 703cbe0 commit f770374

File tree

1 file changed

+16
-0
lines changed
  • kth-smallest-element-in-a-bst

1 file changed

+16
-0
lines changed

kth-smallest-element-in-a-bst/Raft.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
3+
stack = []
4+
5+
while True:
6+
while root:
7+
stack.append(root)
8+
root = root.left
9+
root = stack.pop()
10+
k -= 1
11+
if not k:
12+
return root.val
13+
root = root.right
14+
# T: O(n)
15+
# S: O(n)
16+

0 commit comments

Comments
 (0)