Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fdfa50b

Browse files
Merge branch 'youngyangyang04:master' into master
2 parents 31e6a1a + 4f7e3d9 commit fdfa50b

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

‎problems/0108.将有序数组转换为二叉搜索树.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,38 @@ class Solution:
352352
return mid_root
353353
```
354354

355+
**迭代**(左闭右开)
356+
```python
357+
class Solution:
358+
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
359+
if len(nums) == 0: return None
360+
root = TreeNode() # 初始化
361+
nodeSt = [root]
362+
leftSt = [0]
363+
rightSt = [len(nums)]
364+
365+
while nodeSt:
366+
node = nodeSt.pop() # 处理根节点
367+
left = leftSt.pop()
368+
right = rightSt.pop()
369+
mid = left + (right - left) // 2
370+
node.val = nums[mid]
371+
372+
if left < mid: # 处理左区间
373+
node.left = TreeNode()
374+
nodeSt.append(node.left)
375+
leftSt.append(left)
376+
rightSt.append(mid)
377+
378+
if right > mid + 1: # 处理右区间
379+
node.right = TreeNode()
380+
nodeSt.append(node.right)
381+
leftSt.append(mid + 1)
382+
rightSt.append(right)
383+
384+
return root
385+
```
386+
355387
## Go
356388

357389
递归(隐含回溯)

‎problems/0450.删除二叉搜索树中的节点.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,24 @@ class Solution:
348348
return root
349349
```
350350

351+
**普通二叉树的删除方式**
352+
```python
353+
class Solution:
354+
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
355+
if not root: return root
356+
if root.val == key:
357+
if not root.right: # 这里第二次操作目标值:最终删除的作用
358+
return root.left
359+
tmp = root.right
360+
while tmp.left:
361+
tmp = tmp.left
362+
root.val, tmp.val = tmp.val, root.val # 这里第一次操作目标值:交换目标值其右子树最左面节点。
363+
364+
root.left = self.deleteNode(root.left, key)
365+
root.right = self.deleteNode(root.right, key)
366+
return root
367+
```
368+
351369
**迭代法**
352370
```python
353371
class Solution:

‎problems/0669.修剪二叉搜索树.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,32 @@ class Solution:
299299
return root
300300
```
301301

302+
**迭代**
303+
```python
304+
class Solution:
305+
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
306+
if not root: return root
307+
# 处理头结点,让root移动到[L, R] 范围内,注意是左闭右开
308+
while root and (root.val < low or root.val > high):
309+
if root.val < low: # 小于L往右走
310+
root = root.right
311+
else: # 大于R往左走
312+
root = root.left
313+
# 此时root已经在[L, R] 范围内,处理左孩子元素小于L的情况
314+
cur = root
315+
while cur:
316+
while cur.left and cur.left.val < low:
317+
cur.left = cur.left.right
318+
cur = cur.left
319+
# 此时root已经在[L, R] 范围内,处理右孩子大于R的情况
320+
cur = root
321+
while cur:
322+
while cur.right and cur.right.val > high:
323+
cur.right = cur.right.left
324+
cur = cur.right
325+
return root
326+
```
327+
302328
## Go
303329

304330
```go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /