Skip to content

Commit 57582b4

Browse files
committed
添加题450,110
1 parent 6f3d44d commit 57582b4

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

‎算法思维/二叉搜索树.md‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,73 @@ function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null{
4343
};
4444
```
4545

46+
##### [450. 删除二叉搜索树中的节点](https://leetcode-cn.com/problems/delete-node-in-a-bst/)
47+
48+
给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
49+
50+
> 思路:[秒懂就完事了](https://leetcode-cn.com/problems/delete-node-in-a-bst/solution/miao-dong-jiu-wan-shi-liao-by-terry2020-tc0o/)
51+
52+
```tsx
53+
54+
function deleteNode(root:TreeNode|null, key:number):TreeNode|null{
55+
if (root===null){
56+
returnroot
57+
}
58+
if (root.val<key){
59+
root.right=deleteNode(root.right, key)
60+
} elseif (root.val>key){
61+
root.left=deleteNode(root.left, key)
62+
} else{
63+
if (root.left===null){
64+
returnroot.right
65+
} elseif (root.right===null){
66+
returnroot.left
67+
} else{
68+
let cur:TreeNode=root.right
69+
while (cur.left!==null){
70+
cur=cur.left
71+
}
72+
cur.left=root.left
73+
returnroot.right
74+
}
75+
}
76+
returnroot
77+
};
78+
```
79+
80+
##### [110. 平衡二叉树](https://leetcode-cn.com/problems/balanced-binary-tree/)
81+
82+
给定一个二叉树,判断它是否是高度平衡的二叉树。
83+
84+
```js
85+
constisBalanced=function (root){
86+
if (maxDepth(root) ===-1){
87+
returnfalse
88+
}
89+
returntrue
90+
};
91+
92+
constmaxDepth=function (root){
93+
if (root ===null){
94+
return0
95+
}
96+
constleft=maxDepth(root.left)
97+
constright=maxDepth(root.right)
98+
if (left ===-1|| right ===-1||Math.abs(right - left) >1){
99+
return-1
100+
}
101+
if (left > right){
102+
return left +1
103+
} else{
104+
return right +1
105+
}
106+
}
107+
```
108+
109+
## 练习
110+
111+
-[validate-binary-search-tree](https://leetcode-cn.com/problems/validate-binary-search-tree/)
112+
-[insert-into-a-binary-search-tree](https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/)
113+
-[delete-node-in-a-bst](https://leetcode-cn.com/problems/delete-node-in-a-bst/)
114+
-[balanced-binary-tree](https://leetcode-cn.com/problems/balanced-binary-tree/)
115+

0 commit comments

Comments
(0)