forked from greyireland/algorithm-pattern
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbinary_search_tree.md
166 lines (143 loc) · 4.4 KB
/
binary_search_tree.md
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
155
156
157
158
159
160
161
162
163
164
165
166
# 二叉搜索树
## 定义
- 每个节点中的值必须大于(或等于)存储在其左侧子树中的任何值。
- 每个节点中的值必须小于(或等于)存储在其右子树中的任何值。
## 应用
[validate-binary-search-tree](https://leetcode-cn.com/problems/validate-binary-search-tree/)
> 验证二叉搜索树
```c++
struct Result {
TreeNode *maxNode;
TreeNode *minNode;
bool isValidate;
Result(bool validate = true, TreeNode *max = nullptr, TreeNode *min = nullptr)
: isValidate(validate), maxNode(max), minNode(min) {
}
};
bool isValidBST(TreeNode *root) {
if (!root) {
return true;
}
return helper(root).isValidate;
}
Result helper(TreeNode *root) {
if (!root) {
return {};
}
auto left = helper(root->left);
auto right = helper(root->right);
if (!(left.isValidate && right.isValidate)) {
return {false};
}
if (left.maxNode && left.maxNode->val >= root->val) {
return {false};
}
if (right.minNode && right.minNode->val <= root->val) {
return {false};
}
return {
true,
right.maxNode ? right.maxNode : root,
left.minNode ? left.minNode : root,
};
}
```
[insert-into-a-binary-search-tree](https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/)
> 给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 保证原始二叉搜索树中不存在新值。
```c++
TreeNode *insertIntoBST(TreeNode *root, int val) {
if (root == nullptr) {
return new TreeNode(val);
}
if (root->val > val) {
root->left = insertIntoBST(root->left, val);
} else {
root->right = insertIntoBST(root->right, val);
}
return root;
}
```
[delete-node-in-a-bst](https://leetcode-cn.com/problems/delete-node-in-a-bst/)
> 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
```c++
// 注意二叉搜索树的概念!
// 如果当前节点是其父节点的左子节点,则当前节点底下任何一个节点都要比该父节点小
// 反之亦然
TreeNode* deleteNode(TreeNode* root, int key) {
if (root == nullptr) {
return root;
}
if (root->val < key) {
root->right = deleteNode(root->right, key);
return root;
}
if (root->val > key) {
root->left = deleteNode(root->left, key);
return root;
}
// 每个节点中的值必须大于(或等于)存储在其左侧子树中的任何值。
// 所以压根不需要考虑啥当前节点的父节点,必定当前节点的左右子树!
if (root->left == nullptr) {
return root->right;
}
if (root->right == nullptr) {
return root->left;
}
auto iter = root->right;
while (iter->left != nullptr) {
iter = iter->left;
}
iter->left = root->left;
return root->right;
}
```
[balanced-binary-tree](https://leetcode-cn.com/problems/balanced-binary-tree/)
> 给定一个二叉树,判断它是否是高度平衡的二叉树。
```c++
struct ResultType {
int height;
bool valid;
};
bool isBalanced(TreeNode *root) {
return dfs(root).valid;
}
type ResultType struct{
height int
valid bool
}
func isBalanced(root *TreeNode) bool {
return dfs(root).valid
}
func dfs(root *TreeNode)(result ResultType){
if root==nil{
result.valid=true
result.height=0
return
}
left:=dfs(root.Left)
right:=dfs(root.Right)
// 满足所有特点:二叉搜索树&&平衡
if left.valid&&right.valid&&abs(left.height,right.height)<=1{
result.valid=true
}
result.height=Max(left.height,right.height)+1
return
}
func abs(a,b int)int{
if a>b{
return a-b
}
return b-a
}
func Max(a,b int)int{
if a>b{
return a
}
return b
}
```
## 练习
- [ ] [validate-binary-search-tree](https://leetcode-cn.com/problems/validate-binary-search-tree/)
- [ ] [insert-into-a-binary-search-tree](https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/)
- [ ] [delete-node-in-a-bst](https://leetcode-cn.com/problems/delete-node-in-a-bst/)
- [ ] [balanced-binary-tree](https://leetcode-cn.com/problems/balanced-binary-tree/)