From 38b8b343c931019e76d7692b99ee7d397538494c Mon Sep 17 00:00:00 2001 From: zhicheng lee <904688436@qq.com> Date: Thu, 9 Dec 2021 11:16:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E9=A2=98=E7=9B=AE0701=20J?= =?UTF-8?q?ava=E7=89=88=E6=9C=AC=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0701.二叉搜索树中的插入操作.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) 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; }