-
Notifications
You must be signed in to change notification settings - Fork 1
/
230.KthSmallestElementinaBST.py
executable file
·168 lines (143 loc) · 4.19 KB
/
230.KthSmallestElementinaBST.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed May 20 20:08:47 2020
@author: nenad
"""
"""
Problem URL: https://leetcode.com/problems/kth-smallest-element-in-a-bst/
Problem description:
Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
Example 1:
Input: root = [3,1,4,null,2], k = 1
3
/ \
1 4
\
2
Output: 1
Example 2:
Input: root = [5,3,6,2,4,null,null,1], k = 3
5
/ \
3 6
/ \
2 4
/
1
Output: 3
Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
Hide Hint #1
Try to utilize the property of a BST.
Hide Hint #2
Try in-order traversal. (Credits to @chan13)
Hide Hint #3
What if you could modify the BST node's structure?
Hide Hint #4
The optimal runtime complexity is O(height of BST).
"""
# Time: O(height + k), space: O(height + k)
# 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 __init__(self):
self.nodeCount = 0
def kthSmallest(self, root: TreeNode, k: int) -> int:
self.nodeCount = 0
def inorder(root):
if not root: return None
leftRetVal = inorder(root.left)
# when we get non-None value, that means we reached the kth element
if leftRetVal is not None:
return leftRetVal
self.nodeCount += 1
if self.nodeCount == k: return root.val
# when we get non-None value, that means we reached the kth element
rightRetVal = inorder(root.right)
if rightRetVal is not None:
return rightRetVal
return inorder(root)
def kthSmallest(self, root: TreeNode, k: int) -> int:
def inorder(node):
if node is None:
return
result = inorder(node.left)
if result is not None: return result
self.nodeCount += 1
if self.nodeCount == k:return node.val
result = inorder(node.right)
if result is not None: return result
return inorder(root)
sol = Solution()
# Test 1
# n1 = TreeNode(3)
# n2 = TreeNode(1)
# n3 = TreeNode(4)
# n4 = TreeNode(2)
# n1.left = n2
# n1.right = n3
# n2.right = n4
# print(sol.kthSmallest(n1, 1))
# Test 1
n1 = TreeNode(5)
n2 = TreeNode(3)
n3 = TreeNode(6)
n4 = TreeNode(2)
n5 = TreeNode(4)
n6 = TreeNode(1)
n1.left = n2
n1.right = n3
n2.left = n4
n2.right = n5
n4.left = n6
print(sol.kthSmallest(n1, 3))
# 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 __init__(self):
self.nodeCount = 0
def kthSmallest(self, root: TreeNode, k: int) -> int:
def inorder(node):
if node is None:
return
result = inorder(node.left)
if result is not None: return result
self.nodeCount += 1
if self.nodeCount == k:return node.val
result = inorder(node.right)
if result is not None: return result
return inorder(root)
class Solution:
# @type of root: TreeNode
# @type of k: integer
# @return type: TreeNode
count = 0
def kthSmallest(self, root: TreeNode, k: int) -> TreeNode:
# write your awesome code here
Solution.count = 0
def inorderUtils(root, k):
if root is None:
return
value = inorderUtils(root.left,k)
if value is not None:
return value
Solution.count += 1
if k == Solution.count:
return root.val
value = inorderUtils(root.right, k)
if value is not None:
return value
#kthVal = None
return inorderUtils(root, k)