@@ -7,7 +7,7 @@ https://leetcode.cn/problems/insert-into-a-binary-search-tree/
7
7
``` java []
8
8
class Solution {
9
9
public TreeNode insertIntoBST (TreeNode root , int val ) {
10
-
10
+
11
11
if (root == null ) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。
12
12
return new TreeNode (val);
13
13
if (root. val < val){
@@ -31,12 +31,13 @@ class Solution {
31
31
* var right: TreeNode? = null
32
32
* }
33
33
*/
34
+
34
35
class Solution {
35
36
fun insertIntoBST (root : TreeNode ? , `val `: Int ): TreeNode ? {
36
- if (root== null )return TreeNode (`val `)
37
- if (root.`val `> `val `)root.left= insertIntoBST(root.left,`val `)
38
- if (root.`val `< `val `)root.right= insertIntoBST(root.right,`val `)
39
- return root
37
+ if (root == null ) return TreeNode (`val `)
38
+ if (root.`val ` > `val `) root.left = insertIntoBST(root.left, `val `)
39
+ if (root.`val ` < `val `) root.right = insertIntoBST(root.right, `val `)
40
+ return root
40
41
}
41
42
}
42
43
```
@@ -57,11 +58,11 @@ return root
57
58
*/
58
59
59
60
function insertIntoBST(root : TreeNode | null , val : number ): TreeNode | null {
60
- if (root === null ) return { val ,left:null ,right:null }
61
- if (root .val > val ) root .left = insertIntoBST (root .left ,val )
62
- else if (root .val < val ) root .right = insertIntoBST (root .right ,val );
61
+ if (root === null ) return { val , left: null , right: null };
62
+ if (root .val > val ) root .left = insertIntoBST (root .left , val );
63
+ else if (root .val < val ) root .right = insertIntoBST (root .right , val );
63
64
return root ;
64
- };
65
+ }
65
66
```
66
67
67
68
``` go []
@@ -95,10 +96,10 @@ public:
95
96
```
96
97
97
98
```php []
98
- class Solution
99
+ class Solution
99
100
{
100
101
// 递归函数,返回构造后 BST 的根节点
101
- public function insertIntoBST($root, $val)
102
+ public function insertIntoBST($root, $val)
102
103
{
103
104
if ($root === null) return new TreeNode($val);
104
105
@@ -107,7 +108,7 @@ class Solution
107
108
} else {
108
109
$root->right = $this->insertIntoBST($root->right, $val);
109
110
}
110
-
111
+
111
112
return $root;
112
113
}
113
114
}
@@ -130,7 +131,7 @@ class Solution
130
131
public class Solution {
131
132
public TreeNode InsertIntoBST (TreeNode root , int val ) {
132
133
133
-
134
+
134
135
if (root == null ) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。
135
136
return new TreeNode (val );
136
137
if (root .val < val ){
0 commit comments