-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path230.py
More file actions
31 lines (25 loc) · 728 Bytes
/
230.py
File metadata and controls
31 lines (25 loc) · 728 Bytes
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
from typing import Optional
# Definition for a binary tree node.
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:
order = [1]
res = [root.val]
def _dfs(node):
if not node:
return False
if _dfs(node.left):
return True
if order[0] == k:
res[0] = node.val
return True
order[0] += 1
if _dfs(node.right):
return True
return False
_dfs(root)
return res[0]