Skip to content

Commit a952c7d

Browse files
committed
https://leetcode.cn/problems/insert-into-a-binary-search-tree/
1 parent 37a6ec7 commit a952c7d

File tree

8 files changed

+254
-10
lines changed

8 files changed

+254
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/insert-into-a-binary-search-tree/
14+
1315
https://leetcode.cn/problems/adding-two-negabinary-numbers/
1416

1517
https://leetcode.cn/problems/lMSNwu/

add-two-integers/export.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
package add_two_integers
2-
3-
func Sum(num1 int, num2 int) int {
4-
return sum(num1, num2)
5-
}
1+
package add_two_integers
2+
3+
func Sum(num1 int, num2 int) int {
4+
return sum(num1, num2)
5+
}

add-two-integers/index.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
package add_two_integers
2-
3-
func sum(num1 int, num2 int) int {
4-
return num1 + num2
5-
}
1+
package add_two_integers
2+
3+
func sum(num1 int, num2 int) int {
4+
return num1 + num2
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package insert_into_a_binary_search_tree
2+
3+
func InsertIntoBST(root *TreeNode, val int) *TreeNode {
4+
return insertIntoBST(root, val)
5+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package insert_into_a_binary_search_tree
2+
3+
import serialize_and_deserialize_binary_tree "github.com/masx200/leetcode-test/serialize-and-deserialize-binary-tree"
4+
5+
type TreeNode = serialize_and_deserialize_binary_tree.TreeNode
6+
7+
func insertIntoBST(root *TreeNode, val int) *TreeNode {
8+
if root == nil {
9+
root = &TreeNode{Val: val}
10+
return root
11+
}
12+
if root.Val > val {
13+
root.Left = insertIntoBST(root.Left, val)
14+
} else {
15+
root.Right = insertIntoBST(root.Right, val)
16+
}
17+
return root
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package masx200.leetcode_test.insert_into_a_binary_search_tree
2+
3+
class TreeNode(var `val`: Int = 0, var left: TreeNode? = null, var right: TreeNode? = null) {}
4+
5+
class Solution {
6+
fun insertIntoBST(root: TreeNode?, `val`: Int): TreeNode? {
7+
if (root == null) return TreeNode(`val`)
8+
if (root.`val` > `val`) root.left = insertIntoBST(root.left, `val`)
9+
if (root.`val` < `val`) root.right = insertIntoBST(root.right, `val`)
10+
return root
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
2+
3+
export default function insertIntoBST(
4+
root: TreeNode | null,
5+
val: number,
6+
): TreeNode | null {
7+
if (root === null) return { val, left: null, right: null };
8+
if (root.val > val) root.left = insertIntoBST(root.left, val);
9+
else if (root.val < val) root.right = insertIntoBST(root.right, val);
10+
return root;
11+
}
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
701. 二叉搜索树中的插入操作
2+
3+
https://leetcode.cn/problems/insert-into-a-binary-search-tree/
4+
5+
简单递归版本
6+
7+
```java []
8+
class Solution {
9+
public TreeNode insertIntoBST(TreeNode root, int val) {
10+
11+
if (root == null) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。
12+
return new TreeNode(val);
13+
if (root.val < val){
14+
root.right = insertIntoBST(root.right, val); // 递归创建右子树
15+
}else if (root.val > val){
16+
root.left = insertIntoBST(root.left, val); // 递归创建左子树
17+
}
18+
return root;
19+
}
20+
}
21+
```
22+
23+
```kotlin []
24+
/**
25+
* Example:
26+
* var ti = TreeNode(5)
27+
* var v = ti.`val`
28+
* Definition for a binary tree node.
29+
* class TreeNode(var `val`: Int) {
30+
* var left: TreeNode? = null
31+
* var right: TreeNode? = null
32+
* }
33+
*/
34+
class Solution {
35+
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
40+
}
41+
}
42+
```
43+
44+
```typescript []
45+
/**
46+
* Definition for a binary tree node.
47+
* class TreeNode {
48+
* val: number
49+
* left: TreeNode | null
50+
* right: TreeNode | null
51+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
52+
* this.val = (val===undefined ? 0 : val)
53+
* this.left = (left===undefined ? null : left)
54+
* this.right = (right===undefined ? null : right)
55+
* }
56+
* }
57+
*/
58+
59+
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);
63+
return root;
64+
};
65+
```
66+
67+
```go []
68+
func insertIntoBST(root *TreeNode, val int) *TreeNode {
69+
if root == nil {
70+
root = &TreeNode{Val: val}
71+
return root
72+
}
73+
if root.Val > val {
74+
root.Left = insertIntoBST(root.Left, val)
75+
} else {
76+
root.Right = insertIntoBST(root.Right, val)
77+
}
78+
return root
79+
}
80+
```
81+
82+
```cpp []
83+
class Solution {
84+
public:
85+
TreeNode* insertIntoBST(TreeNode* root, int val) {
86+
if (root == NULL) {
87+
TreeNode* node = new TreeNode(val);
88+
return node;
89+
}
90+
if (root->val > val) root->left = insertIntoBST(root->left, val);
91+
if (root->val < val) root->right = insertIntoBST(root->right, val);
92+
return root;
93+
}
94+
};
95+
```
96+
97+
```php []
98+
class Solution
99+
{
100+
// 递归函数,返回构造后 BST 的根节点
101+
public function insertIntoBST($root, $val)
102+
{
103+
if ($root === null) return new TreeNode($val);
104+
105+
if ($val < $root->val) {
106+
$root->left = $this->insertIntoBST($root->left, $val);
107+
} else {
108+
$root->right = $this->insertIntoBST($root->right, $val);
109+
}
110+
111+
return $root;
112+
}
113+
}
114+
```
115+
116+
```csharp []
117+
/**
118+
* Definition for a binary tree node.
119+
* public class TreeNode {
120+
* public int val;
121+
* public TreeNode left;
122+
* public TreeNode right;
123+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
124+
* this.val = val;
125+
* this.left = left;
126+
* this.right = right;
127+
* }
128+
* }
129+
*/
130+
public class Solution {
131+
public TreeNode InsertIntoBST(TreeNode root, int val) {
132+
133+
134+
if (root == null) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。
135+
return new TreeNode(val);
136+
if (root.val < val){
137+
root.right = InsertIntoBST(root.right, val); // 递归创建右子树
138+
}else if (root.val > val){
139+
root.left = InsertIntoBST(root.left, val); // 递归创建左子树
140+
}
141+
return root;
142+
}
143+
}
144+
```
145+
146+
```python []
147+
class Solution:
148+
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
149+
if root is None:
150+
return TreeNode(val) # 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。
151+
if root.val < val:
152+
root.right = self.insertIntoBST(root.right, val) # 递归创建右子树
153+
if root.val > val:
154+
root.left = self.insertIntoBST(root.left, val) # 递归创建左子树
155+
return root
156+
```
157+
158+
```scala []
159+
/**
160+
* Definition for a binary tree node.
161+
* class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
162+
* var value: Int = _value
163+
* var left: TreeNode = _left
164+
* var right: TreeNode = _right
165+
* }
166+
*/
167+
object Solution {
168+
def insertIntoBST(root: TreeNode, `val`: Int): TreeNode = {
169+
if (root != null) {
170+
if (root.value < `val`) {
171+
root.right = insertIntoBST(root.right, `val`)
172+
}
173+
else {
174+
root.left = insertIntoBST(root.left, `val`)
175+
}
176+
} else {
177+
return new TreeNode(`val`)
178+
}
179+
root
180+
}
181+
}
182+
```
183+
184+
```swift []
185+
class Solution {
186+
func insertIntoBST(_ root: TreeNode?, _ val: Int) -> TreeNode? {
187+
if root == nil{return TreeNode(val)}
188+
if val<root!.val{
189+
root?.left=insertIntoBST(root?.left, val)
190+
}else{
191+
root?.right=insertIntoBST(root?.right, val)
192+
}
193+
return root
194+
}
195+
}
196+
```

0 commit comments

Comments
 (0)