mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
更新 题目0701 Java版本代码
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user