@@ -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+ return root
57+ }
58+ if (root .val < key ) {
59+ root .right = deleteNode (root .right , key )
60+ } else if (root .val > key ) {
61+ root .left = deleteNode (root .left , key )
62+ } else {
63+ if (root .left === null ) {
64+ return root .right
65+ } else if (root .right === null ) {
66+ return root .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+ return root .right
74+ }
75+ }
76+ return root
77+ };
78+ ```
79+
80+ ##### [ 110. 平衡二叉树] ( https://leetcode-cn.com/problems/balanced-binary-tree/ )
81+
82+ 给定一个二叉树,判断它是否是高度平衡的二叉树。
83+
84+ ``` js
85+ const isBalanced = function (root ) {
86+ if (maxDepth (root) === - 1 ) {
87+ return false
88+ }
89+ return true
90+ };
91+
92+ const maxDepth = function (root ) {
93+ if (root === null ) {
94+ return 0
95+ }
96+ const left = maxDepth (root .left )
97+ const right = 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