-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path230. Kth Smallest Element in a BST.cpp
126 lines (105 loc) · 2.37 KB
/
230. Kth Smallest Element in a BST.cpp
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
/*
Link: https://leetcode.com/problems/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?
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// Solution 1: Using recursion and extra space O(n)
/*
TC = O(n)
SC = O(n)
*/
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
vector<int> v;
inOrder(root, v);
return v[k - 1];
}
void inOrder (TreeNode * root, vector<int> &v) {
if (!root) {
return;
}
inOrder (root -> left, v);
v.push_back(root -> val);
inOrder (root -> right, v);
}
};
// Solution 2 : Using recursion and constant space
/*
TC = O(n)
SC = O(1)
*/
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
int ans = 0;
inOrder (root, k, ans);
return ans;
}
void inOrder (TreeNode * root, int &k, int &ans) {
if (!root) {
return;
}
inOrder (root -> left, k, ans);
if (--k == 0) {
ans = root -> val;
return;
}
inOrder (root -> right, k, ans);
}
};
// Solution 3 : Using stack
/*
TC = O(n)
SC = O(n)
*/
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode *> st;
TreeNode * current = root;
while(current || !st.empty()) {
while (current) {
st.push(current);
current = current -> left;
}
current = st.top();
st. pop();
if (--k == 0) {
return current -> val;
}
current = current -> right;
}
return 0;
}
};