-
Notifications
You must be signed in to change notification settings - Fork 0
/
52_kth-smallest-element-in-a-bst.cpp
69 lines (58 loc) · 1.59 KB
/
52_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
// DATE: 07-Aug-2023
/* PROGRAM: 52_Tree - Kth Smallest Element in a BST
https://leetcode.com/problems/kth-smallest-element-in-a-bst/
Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed)
of all the values of the nodes in the tree.
Example 1:
Input: root = [3,1,4,null,2], k = 1
Output: 1
Example 2:
Input: root = [5,3,6,2,4,null,null,1], k = 3
Output: 3
*/
// @ankitsamaddar @Aug_2023
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
int n = 0;
stack<TreeNode*> stack;
stack.push(root);
TreeNode* cur = root->left;
while (cur || !stack.empty()) {
while (cur) {
stack.push(cur);
cur = cur->left;
}
cur = stack.top(), stack.pop();
;
n++;
if (n == k) {
return cur->val;
}
cur = cur->right;
}
return 0;
}
};
int main() {
TreeNode* root = new TreeNode(3);
root->left = new TreeNode(1);
root->right = new TreeNode(4);
root->left->right = new TreeNode(2);
int k = 1;
Solution sol;
cout << sol.kthSmallest(root, k);
return 0;
}