/
smallest.py
35 lines (31 loc) · 920 Bytes
/
smallest.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
from typing import List, Optional
class TreeNode():
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
ans = None
count = 0
def visit(node):
nonlocal ans, count
if not node:
return
visit(node.left)
count += 1
if count == k:
ans = node.val
visit(node.right)
visit(root)
return ans
# Checking in terminal/console:
if __name__ == '__main__':
Instant = Solution()
root = TreeNode(3)
root.left = TreeNode(1)
root.right = TreeNode(4)
# root.left.left = TreeNode()
root.left.right = TreeNode(2)
Solve = Instant.kthSmallest(root, k = 1) # root = [3,1,4,null,2], k = 1 -> 1
print(Solve)