更新 题目0701 Java版本代码

This commit is contained in:
zhicheng lee
2021-12-09 11:16:22 +08:00
committed by GitHub
parent e18449e7ed
commit 38b8b343c9

View File

@ -236,16 +236,13 @@ class Solution {
```java ```java
class Solution { class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) { public TreeNode insertIntoBST(TreeNode root, int val) {
return buildTree(root, val);
}
public TreeNode buildTree(TreeNode root, int val){
if (root == null) // 如果当前节点为空也就意味着val找到了合适的位置此时创建节点直接返回。 if (root == null) // 如果当前节点为空也就意味着val找到了合适的位置此时创建节点直接返回。
return new TreeNode(val); return new TreeNode(val);
if (root.val < val){ if (root.val < val){
root.right = buildTree(root.right, val); // 递归创建右子树 root.right = insertIntoBST(root.right, val); // 递归创建右子树
}else if (root.val > val){ }else if (root.val > val){
root.left = buildTree(root.left, val); // 递归创建左子树 root.left = insertIntoBST(root.left, val); // 递归创建左子树
} }
return root; return root;
} }