-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path54_Kth_Node_BST.cpp
More file actions
156 lines (114 loc) · 3.58 KB
/
54_Kth_Node_BST.cpp
File metadata and controls
156 lines (114 loc) · 3.58 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
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
//
// Created by mark on 2019/7/25.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:54. 二叉搜索树的第k大节点
2. 思路:二叉搜索树的中序遍历为递增序列,中序遍历,然后找第k个即可。
*/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <assert.h>
#include <cstdio>
#include <fstream>
#include <map>
#include <set>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
};
class solutioin
{
int count = 0; // 记录第几个值
public:
// 1. 递归写法
TreeNode* KthNode_Recur(TreeNode* root, int k)
{
if(root == nullptr)
return nullptr;
TreeNode* res = nullptr;
if((res = KthNode_Recur(root->left, k)) != nullptr) // 递归遍历左子树,是否为nullptr
return res;
count++;
if(count == k) return root; // 遍历当前节点
if((res = KthNode_Recur(root->right, k)) != nullptr) // 递归右子树
return res;
return nullptr;
}
// 2. 非递归写法.借助栈,对任一root,如果左节点不为空,入栈root,且root=root->left;如果左节点为空,弹出节点访问,看是否满足条件;不满足root=root->right;直到栈空。
TreeNode* KthNode(TreeNode* root, int k)
{
if(root == nullptr)
return nullptr;
stack<TreeNode*> s;
TreeNode* node = root;
int count = 0;
while(node != nullptr || !s.empty()) // 开始遍历树
{
while(node != nullptr) // 一直访问左子树
{
s.push(node);
node = node->left;
}
if(!s.empty())
{
node = s.top();
count++;
if(count == k) return node; // 判断是否找到第k个节点
s.pop();
node = node->right;
}
}
return nullptr;
}
//辅助函数 ------------------------------------------------------------------------------------------------------------------------
// 构建树节点
TreeNode* CreateTreeNode(int val)
{
TreeNode* pNode = new TreeNode();
pNode->val = val;
pNode->left = nullptr;
pNode->right = nullptr;
return pNode;
}
// 连接树节点
void ConnectTreeNodes(TreeNode* pParent, TreeNode* pLeft, TreeNode* pRight)
{
if(pParent != nullptr)
{
pParent->left = pLeft;
pParent->right = pRight;
}
}
};
int main()
{
solutioin solu;
// 8
// 6 10
// 5 7 9 11
TreeNode* pNode8 = solu.CreateTreeNode(8);
TreeNode* pNode6 = solu.CreateTreeNode(6);
TreeNode* pNode10 = solu.CreateTreeNode(10);
TreeNode* pNode5 = solu.CreateTreeNode(5);
TreeNode* pNode7 = solu.CreateTreeNode(7);
TreeNode* pNode9 = solu.CreateTreeNode(9);
TreeNode* pNode11 = solu.CreateTreeNode(11);
solu.ConnectTreeNodes(pNode8, pNode6, pNode10);
solu.ConnectTreeNodes(pNode6, pNode5, pNode7);
solu.ConnectTreeNodes(pNode10, pNode9, pNode11);
cout << "输入K值(小于8):";
int k;
cin >> k;
TreeNode* res = solu.KthNode_Recur(pNode8, k);
cout << "第k个节点值是:" << res->val << endl;
return 0;
}