Skip to content

Commit bc156f3

Browse files
committed
[fix]添加二叉搜索树
1 parent d80b8a8 commit bc156f3

File tree

1 file changed

+109
-120
lines changed

1 file changed

+109
-120
lines changed

advanced_algorithm/binary_search_tree.md

Lines changed: 109 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -11,160 +11,149 @@
1111

1212
> 验证二叉搜索树
1313
14-
```go
14+
```dart
1515
/**
1616
* Definition for a binary tree node.
17-
* type TreeNode struct {
18-
* Val int
19-
* Left *TreeNode
20-
* Right *TreeNode
17+
* class TreeNode {
18+
* int val;
19+
* TreeNode? left;
20+
* TreeNode? right;
21+
* TreeNode([this.val = 0, this.left, this.right]);
2122
* }
2223
*/
23-
func isValidBST(root *TreeNode) bool {
24-
return dfs(root).valid
24+
class Solution {
25+
int min = -1<<63;
26+
bool isValid = true;
27+
bool isValidBST(TreeNode? root) {
28+
dfs(root);
29+
return isValid;
30+
}
31+
32+
void dfs(TreeNode? root){
33+
if(root == null){
34+
return ;
35+
}
36+
dfs(root.left);
37+
if(root.val > min){
38+
min = root.val;
39+
} else {
40+
isValid = false;
41+
}
42+
dfs(root.right);
43+
}
2544
}
26-
type ResultType struct{
27-
max int
28-
min int
29-
valid bool
30-
}
31-
func dfs(root *TreeNode)(result ResultType){
32-
if root==nil{
33-
result.max=-1<<63
34-
result.min=1<<63-1
35-
result.valid=true
36-
return
37-
}
38-
39-
left:=dfs(root.Left)
40-
right:=dfs(root.Right)
41-
42-
// 1、满足左边最大值<root<右边最小值 && 左右两边valid
43-
if root.Val>left.max && root.Val<right.min && left.valid && right.valid {
44-
result.valid=true
45-
}
46-
// 2、更新当前节点的最大最小值
47-
result.max=Max(Max(left.max,right.max),root.Val)
48-
result.min=Min(Min(left.min,right.min),root.Val)
49-
return
50-
}
51-
func Max(a,b int)int{
52-
if a>b{
53-
return a
54-
}
55-
return b
56-
}
57-
func Min(a,b int)int{
58-
if a>b{
59-
return b
60-
}
61-
return a
62-
}
63-
6445
```
6546

6647
[insert-into-a-binary-search-tree](https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/)
6748

6849
> 给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 保证原始二叉搜索树中不存在新值。
6950
70-
```go
71-
func insertIntoBST(root *TreeNode, val int) *TreeNode {
72-
if root==nil{
73-
return &TreeNode{Val:val}
74-
}
75-
if root.Val<val{
76-
root.Right=insertIntoBST(root.Right,val)
77-
}else{
78-
root.Left=insertIntoBST(root.Left,val)
79-
}
80-
return root
51+
> 如果该子树不为空,则问题转化成了将 val 插入到对应子树上。
52+
> 否则,在此处新建一个以 val 为值的节点,并链接到其父节点 root 上。
53+
54+
```dart
55+
/**
56+
* Definition for a binary tree node.
57+
* class TreeNode {
58+
* int val;
59+
* TreeNode? left;
60+
* TreeNode? right;
61+
* TreeNode([this.val = 0, this.left, this.right]);
62+
* }
63+
*/
64+
class Solution {
65+
TreeNode? insertIntoBST(TreeNode? root, int val) {
66+
if(root == null){
67+
return TreeNode(val);
68+
}
69+
if(root.val < val){
70+
root.right = insertIntoBST(root.right,val);
71+
} else {
72+
root.left = insertIntoBST(root.left,val);
73+
}
74+
return root;
75+
76+
}
8177
}
8278
```
8379

8480
[delete-node-in-a-bst](https://leetcode-cn.com/problems/delete-node-in-a-bst/)
8581

8682
> 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的  key  对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
8783
88-
```go
84+
``` dart
8985
/**
9086
* Definition for a binary tree node.
91-
* type TreeNode struct {
92-
* Val int
93-
* Left *TreeNode
94-
* Right *TreeNode
87+
* class TreeNode {
88+
* int val;
89+
* TreeNode? left;
90+
* TreeNode? right;
91+
* TreeNode([this.val = 0, this.left, this.right]);
9592
* }
9693
*/
97-
func deleteNode(root *TreeNode, key int) *TreeNode {
94+
class Solution {
9895
// 删除节点分为三种情况:
9996
// 1、只有左节点 替换为右
10097
// 2、只有右节点 替换为左
10198
// 3、有左右子节点 左子节点连接到右边最左节点即可
102-
if root ==nil{
103-
return root
104-
}
105-
if root.Val<key{
106-
root.Right=deleteNode(root.Right,key)
107-
}else if root.Val>key{
108-
root.Left=deleteNode(root.Left,key)
109-
}else if root.Val==key{
110-
if root.Left==nil{
111-
return root.Right
112-
}else if root.Right==nil{
113-
return root.Left
114-
}else{
115-
cur:=root.Right
116-
// 一直向左找到最后一个左节点即可
117-
for cur.Left!=nil{
118-
cur=cur.Left
119-
}
120-
cur.Left=root.Left
121-
return root.Right
122-
}
123-
}
124-
return root
99+
TreeNode? deleteNode(TreeNode? root, int key) {
100+
if(root == null) {
101+
return root;
102+
}
103+
if(root.val > key){
104+
root.left = deleteNode(root.left,key);
105+
} else if(root.val < key){
106+
root.right = deleteNode(root.right,key);
107+
} else if(root.val == key){
108+
if(root.right == null){
109+
return root.left;
110+
}else if(root.left == null){
111+
return root.right;
112+
} else {
113+
TreeNode? cur = root.right;
114+
while(cur?.left != null){
115+
cur = cur?.left;
116+
}
117+
cur?.left = root.left;
118+
return root.right;
119+
}
120+
}
121+
return root;
122+
}
125123
}
126124
```
127125

128126
[balanced-binary-tree](https://leetcode-cn.com/problems/balanced-binary-tree/)
129127

130128
> 给定一个二叉树,判断它是否是高度平衡的二叉树。
131129
132-
```go
133-
type ResultType struct{
134-
height int
135-
valid bool
136-
}
137-
func isBalanced(root *TreeNode) bool {
138-
return dfs(root).valid
139-
}
140-
func dfs(root *TreeNode)(result ResultType){
141-
if root==nil{
142-
result.valid=true
143-
result.height=0
144-
return
145-
}
146-
left:=dfs(root.Left)
147-
right:=dfs(root.Right)
148-
// 满足所有特点:二叉搜索树&&平衡
149-
if left.valid&&right.valid&&abs(left.height,right.height)<=1{
150-
result.valid=true
151-
}
152-
result.height=Max(left.height,right.height)+1
153-
return
154-
}
155-
func abs(a,b int)int{
156-
if a>b{
157-
return a-b
158-
}
159-
return b-a
160-
}
161-
func Max(a,b int)int{
162-
if a>b{
163-
return a
164-
}
165-
return b
130+
```dart
131+
/**
132+
* Definition for a binary tree node.
133+
* class TreeNode {
134+
* int val;
135+
* TreeNode? left;
136+
* TreeNode? right;
137+
* TreeNode([this.val = 0, this.left, this.right]);
138+
* }
139+
*/
140+
class Solution {
141+
bool isBalanced(TreeNode? root) {
142+
if(root == null){
143+
return true;
144+
} else {
145+
return (height(root.left) - height(root.right)).abs() <= 1 && isBalanced(root.left) && isBalanced(root.right);
146+
}
147+
}
148+
149+
int height(TreeNode? root) {
150+
if(root == null){
151+
return 0;
152+
} else {
153+
return max(height(root.left),height(root.right)) +1;
154+
}
155+
}
166156
}
167-
168157
```
169158

170159
## 练习

0 commit comments

Comments
 (0)