-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path230.cpp
More file actions
77 lines (62 loc) · 1.79 KB
/
Copy path230.cpp
File metadata and controls
77 lines (62 loc) · 1.79 KB
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
//
// Created by chenyufan on 2020/5/19.
//
/**
* Definition for a binary tree node.
* 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) {}
* };
*/
#include <iostream>
#include <vector>
#include <queue>
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) {}
};
using namespace std;
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
return _kthSmallest(root, k).first;
}
pair<int, int> _kthSmallest(TreeNode* root, int k) {
if (root == nullptr) return {INT_MIN, 0};
auto res = _kthSmallest(root->left, k);
int found = res.first, leftCnt = res.second;
if (found) return {found, 0};
if (k == leftCnt) return {root->val, 0};
res = _kthSmallest(root->right, k - leftCnt - 1);
found = res.first;
int rightCnt = res.second;
if (found) return {found, 0};
return {INT_MIN, leftCnt+rightCnt+1};
}
};
int main() {
TreeNode *root;
root = new TreeNode(3);
vector<int> a{1,4,0,2};
queue<TreeNode*> q;
q.push(root);
int i = 0;
while (i < a.size()) {
auto cur = q.front();
cur->val = a[i];
q.push(cur->left);
q.push(cur->right);
q.pop();
cur = new TreeNode(a[i]);
}
Solution s;
s.kthSmallest(root, 1);
}