mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
Merge pull request #2714 from wq-win/master
修改 0701.二叉搜索树中的插入操作,增加python版本的迭代法(精简),修正 0701.二叉搜索树中的插入操作,递归法版本二应该是迭代法
This commit is contained in:
@ -283,32 +283,10 @@ class Solution:
|
|||||||
return TreeNode(val)
|
return TreeNode(val)
|
||||||
self.traversal(root, val)
|
self.traversal(root, val)
|
||||||
return root
|
return root
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
递归法(版本二)
|
递归法(版本二)
|
||||||
```python
|
```python
|
||||||
class Solution:
|
|
||||||
def insertIntoBST(self, root, val):
|
|
||||||
if root is None:
|
|
||||||
return TreeNode(val)
|
|
||||||
parent = None
|
|
||||||
cur = root
|
|
||||||
while cur:
|
|
||||||
parent = cur
|
|
||||||
if val < cur.val:
|
|
||||||
cur = cur.left
|
|
||||||
else:
|
|
||||||
cur = cur.right
|
|
||||||
if val < parent.val:
|
|
||||||
parent.left = TreeNode(val)
|
|
||||||
else:
|
|
||||||
parent.right = TreeNode(val)
|
|
||||||
return root
|
|
||||||
```
|
|
||||||
|
|
||||||
递归法(版本三)
|
|
||||||
```python
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
||||||
if root is None or root.val == val:
|
if root is None or root.val == val:
|
||||||
@ -326,7 +304,7 @@ class Solution:
|
|||||||
return root
|
return root
|
||||||
```
|
```
|
||||||
|
|
||||||
递归法(版本四)
|
递归法(版本三)
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def insertIntoBST(self, root, val):
|
def insertIntoBST(self, root, val):
|
||||||
@ -340,10 +318,9 @@ class Solution:
|
|||||||
root.right = self.insertIntoBST(root.right, val)
|
root.right = self.insertIntoBST(root.right, val)
|
||||||
|
|
||||||
return root
|
return root
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
迭代法
|
迭代法(版本一)
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def insertIntoBST(self, root, val):
|
def insertIntoBST(self, root, val):
|
||||||
@ -366,10 +343,53 @@ class Solution:
|
|||||||
else:
|
else:
|
||||||
parent.right = node # 将新节点连接到父节点的右子树
|
parent.right = node # 将新节点连接到父节点的右子树
|
||||||
|
|
||||||
return root
|
return root
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
迭代法(版本二)
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def insertIntoBST(self, root, val):
|
||||||
|
if root is None:
|
||||||
|
return TreeNode(val)
|
||||||
|
parent = None
|
||||||
|
cur = root
|
||||||
|
while cur:
|
||||||
|
parent = cur
|
||||||
|
if val < cur.val:
|
||||||
|
cur = cur.left
|
||||||
|
else:
|
||||||
|
cur = cur.right
|
||||||
|
if val < parent.val:
|
||||||
|
parent.left = TreeNode(val)
|
||||||
|
else:
|
||||||
|
parent.right = TreeNode(val)
|
||||||
|
return root
|
||||||
|
```
|
||||||
|
|
||||||
|
迭代法(精简)
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def insertIntoBST(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
||||||
|
if not root: # 如果根节点为空,创建新节点作为根节点并返回
|
||||||
|
return TreeNode(val)
|
||||||
|
cur = root
|
||||||
|
while cur:
|
||||||
|
if val < cur.val:
|
||||||
|
if not cur.left: # 如果此时父节点的左子树为空
|
||||||
|
cur.left = TreeNode(val) # 将新节点连接到父节点的左子树
|
||||||
|
return root
|
||||||
|
else:
|
||||||
|
cur = cur.left
|
||||||
|
elif val > cur.val:
|
||||||
|
if not cur.right: # 如果此时父节点的左子树为空
|
||||||
|
cur.right = TreeNode(val) # 将新节点连接到父节点的右子树
|
||||||
|
return root
|
||||||
|
else:
|
||||||
|
cur = cur.right
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
-----
|
-----
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user