-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy path230.py
More file actions
34 lines (28 loc) · 735 Bytes
/
230.py
File metadata and controls
34 lines (28 loc) · 735 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
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.
# ****************
class Solution(object):
def kthSmallest(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: int
"""
self.k = k
self.res = 0
def dfs(root):
# Edge/Condition
if not root:
return 0
dfs(root.left)
self.k -= 1
if self.k == 0:
self.res = root.val
dfs(root.right)
dfs(root)
return self.res