diff --git a/problems/0701.二叉搜索树中的插入操作.md b/problems/0701.二叉搜索树中的插入操作.md index 5d63ce58..1e6ab47e 100644 --- a/problems/0701.二叉搜索树中的插入操作.md +++ b/problems/0701.二叉搜索树中的插入操作.md @@ -236,16 +236,13 @@ class Solution { ```java class Solution { public TreeNode insertIntoBST(TreeNode root, int val) { - return buildTree(root, val); - } - - public TreeNode buildTree(TreeNode root, int val){ if (root == null) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。 return new TreeNode(val); + if (root.val < val){ - root.right = buildTree(root.right, val); // 递归创建右子树 + root.right = insertIntoBST(root.right, val); // 递归创建右子树 }else if (root.val > val){ - root.left = buildTree(root.left, val); // 递归创建左子树 + root.left = insertIntoBST(root.left, val); // 递归创建左子树 } return root; }