-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkth_smallest_BST.py
35 lines (27 loc) · 960 Bytes
/
kth_smallest_BST.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
# Leetcode: https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/
# Once you know you can get the ascedning order of a tree using an inorder traversal it's easy
# Just need to draw it out a few times
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def kthSmallest(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: int
"""
# Inorder traversal will get you an ascending array
if not root: return None
cur = []
self.inorder(root, cur)
return cur[k - 1]
def inorder(self, root, order):
if (root == None):
return None
self.inorder(root.left, order)
order.append(root.val)
self.inorder(root.right, order)