Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions problems/0701.二叉搜索树中的插入操作.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down