Skip to content

Commit f93906b

Browse files
authored
Create 230-Kth-Smallest-Element-in-a-BST.py
1 parent cec67d4 commit f93906b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

230-Kth-Smallest-Element-in-a-BST.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def kthSmallest(self, root: TreeNode, k: int) -> int:
10+
stack = []
11+
curr = root
12+
13+
while stack or curr:
14+
while curr:
15+
stack.append(curr)
16+
curr = curr.left
17+
curr = stack.pop()
18+
k -= 1
19+
if k == 0:
20+
return curr.val
21+
curr = curr.right

0 commit comments

Comments
 (0)