- Notifications
You must be signed in to change notification settings - Fork 306
/
Copy path230.py
34 lines (28 loc) · 735 Bytes
/
230.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Yu Zhou
# ****************
# Descrption:
# 230. Kth Smallest Element in a BST
# Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
# ****************
classSolution(object):
defkthSmallest(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: int
"""
self.k=k
self.res=0
defdfs(root):
# Edge/Condition
ifnotroot:
return0
dfs(root.left)
self.k-=1
ifself.k==0:
self.res=root.val
dfs(root.right)
dfs(root)
returnself.res