Update 0701.二叉搜索树中的插入操作.md

Python 递归法- 无返回值的一种更简单的实现。
This commit is contained in:
Guang-Hou
2022-02-09 16:37:44 -05:00
committed by GitHub
parent beeff54cc1
commit 7d8e9e8fae

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