diff --git "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" index 468f26750d..5e9fbdfe7e 100644 --- "a/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" +++ "b/problems/0701.\344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\347\232\204\346\217\222\345\205\245\346\223\215\344\275\234.md" @@ -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