Merge pull request #1085 from Guang-Hou/patch-1

Update 0701.二叉搜索树中的插入操作.md
This commit is contained in:
程序员Carl
2022-03-01 10:08:52 +08:00
committed by GitHub

View File

@ -310,6 +310,26 @@ class Solution:
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
```
**迭代法**
与无返回值的递归函数的思路大体一致
```python