mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge branch 'master' of github.com:KingArthur0205/leetcode-master
This commit is contained in:
@ -352,6 +352,38 @@ class Solution:
|
|||||||
return mid_root
|
return mid_root
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**迭代**(左闭右开)
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
|
||||||
|
if len(nums) == 0: return None
|
||||||
|
root = TreeNode() # 初始化
|
||||||
|
nodeSt = [root]
|
||||||
|
leftSt = [0]
|
||||||
|
rightSt = [len(nums)]
|
||||||
|
|
||||||
|
while nodeSt:
|
||||||
|
node = nodeSt.pop() # 处理根节点
|
||||||
|
left = leftSt.pop()
|
||||||
|
right = rightSt.pop()
|
||||||
|
mid = left + (right - left) // 2
|
||||||
|
node.val = nums[mid]
|
||||||
|
|
||||||
|
if left < mid: # 处理左区间
|
||||||
|
node.left = TreeNode()
|
||||||
|
nodeSt.append(node.left)
|
||||||
|
leftSt.append(left)
|
||||||
|
rightSt.append(mid)
|
||||||
|
|
||||||
|
if right > mid + 1: # 处理右区间
|
||||||
|
node.right = TreeNode()
|
||||||
|
nodeSt.append(node.right)
|
||||||
|
leftSt.append(mid + 1)
|
||||||
|
rightSt.append(right)
|
||||||
|
|
||||||
|
return root
|
||||||
|
```
|
||||||
|
|
||||||
## Go
|
## Go
|
||||||
|
|
||||||
递归(隐含回溯)
|
递归(隐含回溯)
|
||||||
|
@ -348,6 +348,24 @@ class Solution:
|
|||||||
return root
|
return root
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**普通二叉树的删除方式**
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def deleteNode(self, root: Optional[TreeNode], key: int) -> Optional[TreeNode]:
|
||||||
|
if not root: return root
|
||||||
|
if root.val == key:
|
||||||
|
if not root.right: # 这里第二次操作目标值:最终删除的作用
|
||||||
|
return root.left
|
||||||
|
tmp = root.right
|
||||||
|
while tmp.left:
|
||||||
|
tmp = tmp.left
|
||||||
|
root.val, tmp.val = tmp.val, root.val # 这里第一次操作目标值:交换目标值其右子树最左面节点。
|
||||||
|
|
||||||
|
root.left = self.deleteNode(root.left, key)
|
||||||
|
root.right = self.deleteNode(root.right, key)
|
||||||
|
return root
|
||||||
|
```
|
||||||
|
|
||||||
**迭代法**
|
**迭代法**
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
|
@ -299,6 +299,32 @@ class Solution:
|
|||||||
return root
|
return root
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**迭代**
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def trimBST(self, root: Optional[TreeNode], low: int, high: int) -> Optional[TreeNode]:
|
||||||
|
if not root: return root
|
||||||
|
# 处理头结点,让root移动到[L, R] 范围内,注意是左闭右开
|
||||||
|
while root and (root.val < low or root.val > high):
|
||||||
|
if root.val < low: # 小于L往右走
|
||||||
|
root = root.right
|
||||||
|
else: # 大于R往左走
|
||||||
|
root = root.left
|
||||||
|
# 此时root已经在[L, R] 范围内,处理左孩子元素小于L的情况
|
||||||
|
cur = root
|
||||||
|
while cur:
|
||||||
|
while cur.left and cur.left.val < low:
|
||||||
|
cur.left = cur.left.right
|
||||||
|
cur = cur.left
|
||||||
|
# 此时root已经在[L, R] 范围内,处理右孩子大于R的情况
|
||||||
|
cur = root
|
||||||
|
while cur:
|
||||||
|
while cur.right and cur.right.val > high:
|
||||||
|
cur.right = cur.right.left
|
||||||
|
cur = cur.right
|
||||||
|
return root
|
||||||
|
```
|
||||||
|
|
||||||
## Go
|
## Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user