mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-12 21:50:49 +08:00
Update 0701.二叉搜索树中的插入操作.md
This commit is contained in:
@ -256,132 +256,103 @@ class Solution {
|
|||||||
-----
|
-----
|
||||||
## Python
|
## Python
|
||||||
|
|
||||||
**递归法** - 有返回值
|
递归法(版本一)
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
|
||||||
# class TreeNode:
|
|
||||||
# def __init__(self, val=0, left=None, right=None):
|
|
||||||
# self.val = val
|
|
||||||
# self.left = left
|
|
||||||
# self.right = right
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
|
def __init__(self):
|
||||||
# 返回更新后的以当前root为根节点的新树,方便用于更新上一层的父子节点关系链
|
self.parent = None
|
||||||
|
|
||||||
# Base Case
|
def traversal(self, cur, val):
|
||||||
if not root: return TreeNode(val)
|
if cur is None:
|
||||||
|
node = TreeNode(val)
|
||||||
|
if val > self.parent.val:
|
||||||
|
self.parent.right = node
|
||||||
|
else:
|
||||||
|
self.parent.left = node
|
||||||
|
return
|
||||||
|
|
||||||
# 单层递归逻辑:
|
self.parent = cur
|
||||||
if val < root.val:
|
if cur.val > val:
|
||||||
# 将val插入至当前root的左子树中合适的位置
|
self.traversal(cur.left, val)
|
||||||
# 并更新当前root的左子树为包含目标val的新左子树
|
if cur.val < val:
|
||||||
root.left = self.insertIntoBST(root.left, val)
|
self.traversal(cur.right, val)
|
||||||
|
|
||||||
if root.val < val:
|
def insertIntoBST(self, root, val):
|
||||||
# 将val插入至当前root的右子树中合适的位置
|
self.parent = TreeNode(0)
|
||||||
# 并更新当前root的右子树为包含目标val的新右子树
|
if root is None:
|
||||||
root.right = self.insertIntoBST(root.right, val)
|
return TreeNode(val)
|
||||||
|
self.traversal(root, val)
|
||||||
# 返回更新后的以当前root为根节点的新树
|
|
||||||
return root
|
return root
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**递归法** - 无返回值
|
递归法(版本二)
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
|
def insertIntoBST(self, root, val):
|
||||||
if not root:
|
if root is None:
|
||||||
return TreeNode(val)
|
return TreeNode(val)
|
||||||
parent = None
|
parent = None
|
||||||
def __traverse(cur: TreeNode, val: int) -> None:
|
|
||||||
# 在函数运行的同时把新节点插入到该被插入的地方.
|
|
||||||
nonlocal parent
|
|
||||||
if not cur:
|
|
||||||
new_node = TreeNode(val)
|
|
||||||
if parent.val < val:
|
|
||||||
parent.right = new_node
|
|
||||||
else:
|
|
||||||
parent.left = new_node
|
|
||||||
return
|
|
||||||
|
|
||||||
parent = cur # 重点: parent的作用只有运行到上面if not cur:才会发挥出来.
|
|
||||||
if cur.val < val:
|
|
||||||
__traverse(cur.right, val)
|
|
||||||
else:
|
|
||||||
__traverse(cur.left, val)
|
|
||||||
return
|
|
||||||
__traverse(root, val)
|
|
||||||
return root
|
|
||||||
```
|
|
||||||
|
|
||||||
**递归法** - 无返回值 - another easier way
|
|
||||||
```python
|
|
||||||
class Solution:
|
|
||||||
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
|
||||||
newNode = TreeNode(val)
|
|
||||||
if not root: return newNode
|
|
||||||
|
|
||||||
if not root.left and val < root.val:
|
|
||||||
root.left = newNode
|
|
||||||
if not root.right and val > root.val:
|
|
||||||
root.right = newNode
|
|
||||||
|
|
||||||
if val < root.val:
|
|
||||||
self.insertIntoBST(root.left, val)
|
|
||||||
if val > root.val:
|
|
||||||
self.insertIntoBST(root.right, val)
|
|
||||||
|
|
||||||
return root
|
|
||||||
```
|
|
||||||
|
|
||||||
**递归法** - 无返回值 有注释 不用Helper function
|
|
||||||
```python
|
|
||||||
class Solution:
|
|
||||||
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
|
||||||
if not root: # for root==None
|
|
||||||
return TreeNode(val)
|
|
||||||
if root.val<val:
|
|
||||||
if root.right==None: # find the parent
|
|
||||||
root.right = TreeNode(val)
|
|
||||||
else: # not found, keep searching
|
|
||||||
self.insertIntoBST(root.right, val)
|
|
||||||
if root.val>val:
|
|
||||||
if root.left==None: # found the parent
|
|
||||||
root.left = TreeNode(val)
|
|
||||||
else: # not found, keep searching
|
|
||||||
self.insertIntoBST(root.left, val)
|
|
||||||
# return the final tree
|
|
||||||
return root
|
|
||||||
```
|
|
||||||
|
|
||||||
**迭代法**
|
|
||||||
与无返回值的递归函数的思路大体一致
|
|
||||||
```python
|
|
||||||
class Solution:
|
|
||||||
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
|
|
||||||
if not root:
|
|
||||||
return TreeNode(val)
|
|
||||||
parent = None # 此步可以省略
|
|
||||||
cur = root
|
cur = root
|
||||||
|
while cur:
|
||||||
# 用while循环不断地找新节点的parent
|
parent = cur
|
||||||
while cur:
|
if val < cur.val:
|
||||||
parent = cur # 首先保存当前非空节点作为下一次迭代的父节点
|
|
||||||
if cur.val < val:
|
|
||||||
cur = cur.right
|
|
||||||
elif cur.val > val:
|
|
||||||
cur = cur.left
|
cur = cur.left
|
||||||
|
else:
|
||||||
# 运行到这意味着已经跳出上面的while循环,
|
cur = cur.right
|
||||||
# 同时意味着新节点的parent已经被找到.
|
if val < parent.val:
|
||||||
# parent已被找到, 新节点已经ready. 把两个节点黏在一起就好了.
|
|
||||||
if parent.val > val:
|
|
||||||
parent.left = TreeNode(val)
|
parent.left = TreeNode(val)
|
||||||
else:
|
else:
|
||||||
parent.right = TreeNode(val)
|
parent.right = TreeNode(val)
|
||||||
|
|
||||||
return root
|
return root
|
||||||
|
```
|
||||||
|
|
||||||
|
递归法(版本三)
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
||||||
|
if root is None or root.val == val:
|
||||||
|
return TreeNode(val)
|
||||||
|
elif root.val > val:
|
||||||
|
if root.left is None:
|
||||||
|
root.left = TreeNode(val)
|
||||||
|
else:
|
||||||
|
self.insertIntoBST(root.left, val)
|
||||||
|
elif root.val < val:
|
||||||
|
if root.right is None:
|
||||||
|
root.right = TreeNode(val)
|
||||||
|
else:
|
||||||
|
self.insertIntoBST(root.right, val)
|
||||||
|
return root
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
迭代法
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def insertIntoBST(self, root, val):
|
||||||
|
if root is None: # 如果根节点为空,创建新节点作为根节点并返回
|
||||||
|
node = TreeNode(val)
|
||||||
|
return node
|
||||||
|
|
||||||
|
cur = root
|
||||||
|
parent = root # 记录上一个节点,用于连接新节点
|
||||||
|
while cur is not None:
|
||||||
|
parent = cur
|
||||||
|
if cur.val > val:
|
||||||
|
cur = cur.left
|
||||||
|
else:
|
||||||
|
cur = cur.right
|
||||||
|
|
||||||
|
node = TreeNode(val)
|
||||||
|
if val < parent.val:
|
||||||
|
parent.left = node # 将新节点连接到父节点的左子树
|
||||||
|
else:
|
||||||
|
parent.right = node # 将新节点连接到父节点的右子树
|
||||||
|
|
||||||
|
return root
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
-----
|
-----
|
||||||
|
Reference in New Issue
Block a user