-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheKthNodeOfBSTree.cpp
More file actions
69 lines (52 loc) · 1.19 KB
/
TheKthNodeOfBSTree.cpp
File metadata and controls
69 lines (52 loc) · 1.19 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
#include <iostream>
#include <vector>
using namespace std;
struct TreeNode {
TreeNode *left;
TreeNode *right;
int val;
TreeNode(void): val(0), left(nullptr), right(nullptr) {}
TreeNode(int val): val(val), left(nullptr), right(nullptr) {}
};
auto GetKthNode(const TreeNode *root, int &k)->int {
if( root == nullptr ) return -1;
if( k == 0 ) return root->val;
if( !root->left && !root->right ) {
k--;
return -1;
}
if( root-> left ) {
int ret = GetKthNode(root->left, k);
if( ret != -1 && k == 0) {
return ret;
}
}
if( k == 0 ) return root->val;
k--;
if( root-> right) {
int ret = GetKthNode(root->right, k);
if( ret != -1 && k == 0) {
return ret;
}
}
return -1;
}
int main(void) {
TreeNode node2(2);
TreeNode node3(3);
TreeNode node4(4);
TreeNode node5(5);
TreeNode node6(6);
TreeNode node7(7);
TreeNode node8(8);
node5.left = &node3;
node3.left = &node2;
node3.right= &node4;
node5.right= &node7;
node7.left = &node6;
node7.right= &node8;
int k = 6-1;
int val = GetKthNode(&node5, k);
cout << "The kth node in BSTree is " << val << endl;
return 0;
}