@@ -32,6 +32,25 @@ class Solution:
3232 return True
3333```
3434
35+ ``` Python
36+ class Solution :
37+ def isValidBST (self , root : TreeNode) -> bool :
38+ if not root:
39+ return True
40+ pre = float (" -inf" )
41+ stack = []
42+ while root or stack:
43+ while root:
44+ stack.append(root)
45+ root = root.left
46+ root = stack.pop()
47+ if pre >= root.val:
48+ return False
49+ pre = root.val
50+ root = root.right
51+ return True
52+ ```
53+
3554### [ insert-into-a-binary-search-tree] ( https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/ )
3655
3756> 给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 保证原始二叉搜索树中不存在新值。
@@ -51,6 +70,27 @@ class Solution:
5170 return root
5271```
5372
73+ ``` Python
74+ class Solution :
75+ def insertIntoBST (self , root : TreeNode, val : int ) -> TreeNode:
76+ if not root:
77+ return TreeNode(val)
78+ node = root
79+ while 1 :
80+ if val > root.val:
81+ if root.right:
82+ root = root.right
83+ else :
84+ root.right = TreeNode(val)
85+ return node
86+ else :
87+ if root.left:
88+ root = root.left
89+ else :
90+ root.left = TreeNode(val)
91+ return node
92+ ```
93+
5494### [ delete-node-in-a-bst] ( https://leetcode-cn.com/problems/delete-node-in-a-bst/ )
5595
5696> 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。
@@ -97,6 +137,28 @@ class Solution:
97137 return dummy.left
98138```
99139
140+ ``` Python
141+ class Solution :
142+ def deleteNode (self , root : TreeNode, key : int ) -> TreeNode:
143+ if not root:
144+ return root
145+ if key > root.val:
146+ root.right = self .deleteNode(root.right, key)
147+ elif key < root.val:
148+ root.left = self .deleteNode(root.left, key)
149+ elif key == root.val:
150+ if not root.left:
151+ return root.right
152+ if not root.right:
153+ return root.left
154+ cur = root.right
155+ while cur.left:
156+ cur = cur.left
157+ cur.left = root.left
158+ return root.right
159+ return root
160+ ```
161+
100162### [ balanced-binary-tree] ( https://leetcode-cn.com/problems/balanced-binary-tree/ )
101163
102164> 给定一个二叉树,判断它是否是高度平衡的二叉树。
@@ -134,6 +196,22 @@ class Solution:
134196 return True
135197```
136198
199+ ``` Python
200+ class Solution :
201+ def isBalanced (self , root : TreeNode) -> bool :
202+ result = [True ]
203+ def helper (root ):
204+ if not root:
205+ return 0
206+ left = helper(root.left)
207+ right = helper(root.right)
208+ if abs (left- right) > 1 :
209+ result[- 1 ] = False
210+ return max (left, right) + 1
211+ _ = helper(root)
212+ return result[- 1 ]
213+ ```
214+
137215### [ valid-bfs-of-bst] ( ./bst_bfs.py )
138216
139217> 给定一个整数数组,求问此数组是不是一个 BST 的 BFS 顺序。
0 commit comments