Skip to content

Commit 05f229e

Browse files
committed
feat: add solutions to lc problem: No.0701
No.0701.Insert into a Binary Search Tree
1 parent 4932c6a commit 05f229e

File tree

6 files changed

+261
-60
lines changed

6 files changed

+261
-60
lines changed

solution/0700-0799/0701.Insert into a Binary Search Tree/README.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,119 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55+
DFS。
56+
5557
<!-- tabs:start -->
5658

5759
### **Python3**
5860

5961
<!-- 这里可写当前语言的特殊实现逻辑 -->
6062

6163
```python
62-
64+
# Definition for a binary tree node.
65+
# class TreeNode:
66+
# def __init__(self, val=0, left=None, right=None):
67+
# self.val = val
68+
# self.left = left
69+
# self.right = right
70+
class Solution:
71+
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
72+
def dfs(root):
73+
if root is None:
74+
return TreeNode(val)
75+
if root.val < val:
76+
root.right = dfs(root.right)
77+
else:
78+
root.left = dfs(root.left)
79+
return root
80+
81+
return dfs(root)
6382
```
6483

6584
### **Java**
6685

6786
<!-- 这里可写当前语言的特殊实现逻辑 -->
6887

6988
```java
89+
/**
90+
* Definition for a binary tree node.
91+
* public class TreeNode {
92+
* int val;
93+
* TreeNode left;
94+
* TreeNode right;
95+
* TreeNode() {}
96+
* TreeNode(int val) { this.val = val; }
97+
* TreeNode(int val, TreeNode left, TreeNode right) {
98+
* this.val = val;
99+
* this.left = left;
100+
* this.right = right;
101+
* }
102+
* }
103+
*/
104+
class Solution {
105+
106+
public TreeNode insertIntoBST(TreeNode root, int val) {
107+
if (root == null) {
108+
return new TreeNode(val);
109+
}
110+
if (root.val < val) {
111+
root.right = insertIntoBST(root.right, val);
112+
} else {
113+
root.left = insertIntoBST(root.left, val);
114+
}
115+
return root;
116+
}
117+
}
118+
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
/**
125+
* Definition for a binary tree node.
126+
* struct TreeNode {
127+
* int val;
128+
* TreeNode *left;
129+
* TreeNode *right;
130+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
131+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
132+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
133+
* };
134+
*/
135+
class Solution {
136+
public:
137+
TreeNode* insertIntoBST(TreeNode* root, int val) {
138+
if (!root) return new TreeNode(val);
139+
if (root->val < val) root->right = insertIntoBST(root->right, val);
140+
else root->left = insertIntoBST(root->left, val);
141+
return root;
142+
}
143+
};
144+
```
70145
146+
### **Go**
147+
148+
```go
149+
/**
150+
* Definition for a binary tree node.
151+
* type TreeNode struct {
152+
* Val int
153+
* Left *TreeNode
154+
* Right *TreeNode
155+
* }
156+
*/
157+
func insertIntoBST(root *TreeNode, val int) *TreeNode {
158+
if root == nil {
159+
return &TreeNode{Val: val}
160+
}
161+
if root.Val < val {
162+
root.Right = insertIntoBST(root.Right, val)
163+
} else {
164+
root.Left = insertIntoBST(root.Left, val)
165+
}
166+
return root
167+
}
71168
```
72169

73170
### **...**

solution/0700-0799/0701.Insert into a Binary Search Tree/README_EN.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,115 @@
4545

4646
## Solutions
4747

48+
DFS.
49+
4850
<!-- tabs:start -->
4951

5052
### **Python3**
5153

5254
```python
53-
55+
# Definition for a binary tree node.
56+
# class TreeNode:
57+
# def __init__(self, val=0, left=None, right=None):
58+
# self.val = val
59+
# self.left = left
60+
# self.right = right
61+
class Solution:
62+
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
63+
def dfs(root):
64+
if root is None:
65+
return TreeNode(val)
66+
if root.val < val:
67+
root.right = dfs(root.right)
68+
else:
69+
root.left = dfs(root.left)
70+
return root
71+
72+
return dfs(root)
5473
```
5574

5675
### **Java**
5776

5877
```java
78+
/**
79+
* Definition for a binary tree node.
80+
* public class TreeNode {
81+
* int val;
82+
* TreeNode left;
83+
* TreeNode right;
84+
* TreeNode() {}
85+
* TreeNode(int val) { this.val = val; }
86+
* TreeNode(int val, TreeNode left, TreeNode right) {
87+
* this.val = val;
88+
* this.left = left;
89+
* this.right = right;
90+
* }
91+
* }
92+
*/
93+
class Solution {
94+
95+
public TreeNode insertIntoBST(TreeNode root, int val) {
96+
if (root == null) {
97+
return new TreeNode(val);
98+
}
99+
if (root.val < val) {
100+
root.right = insertIntoBST(root.right, val);
101+
} else {
102+
root.left = insertIntoBST(root.left, val);
103+
}
104+
return root;
105+
}
106+
}
107+
108+
```
109+
110+
### **C++**
111+
112+
```cpp
113+
/**
114+
* Definition for a binary tree node.
115+
* struct TreeNode {
116+
* int val;
117+
* TreeNode *left;
118+
* TreeNode *right;
119+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
120+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
121+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
122+
* };
123+
*/
124+
class Solution {
125+
public:
126+
TreeNode* insertIntoBST(TreeNode* root, int val) {
127+
if (!root) return new TreeNode(val);
128+
if (root->val < val) root->right = insertIntoBST(root->right, val);
129+
else root->left = insertIntoBST(root->left, val);
130+
return root;
131+
}
132+
};
133+
```
59134
135+
### **Go**
136+
137+
```go
138+
/**
139+
* Definition for a binary tree node.
140+
* type TreeNode struct {
141+
* Val int
142+
* Left *TreeNode
143+
* Right *TreeNode
144+
* }
145+
*/
146+
func insertIntoBST(root *TreeNode, val int) *TreeNode {
147+
if root == nil {
148+
return &TreeNode{Val: val}
149+
}
150+
if root.Val < val {
151+
root.Right = insertIntoBST(root.Right, val)
152+
} else {
153+
root.Left = insertIntoBST(root.Left, val)
154+
}
155+
return root
156+
}
60157
```
61158

62159
### **...**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode* insertIntoBST(TreeNode* root, int val) {
15+
if (!root) return new TreeNode(val);
16+
if (root->val < val) root->right = insertIntoBST(root->right, val);
17+
else root->left = insertIntoBST(root->left, val);
18+
return root;
19+
}
20+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func insertIntoBST(root *TreeNode, val int) *TreeNode {
10+
if root == nil {
11+
return &TreeNode{Val: val}
12+
}
13+
if root.Val < val {
14+
root.Right = insertIntoBST(root.Right, val)
15+
} else {
16+
root.Left = insertIntoBST(root.Left, val)
17+
}
18+
return root
19+
}

solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@
44
* int val;
55
* TreeNode left;
66
* TreeNode right;
7-
* TreeNode(int x) { val = x; }
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
814
* }
915
*/
1016
class Solution {
1117
public TreeNode insertIntoBST(TreeNode root, int val) {
12-
13-
if(root == null){
14-
root = new TreeNode(val);
15-
}
16-
17-
if(val < root.val){
18-
root.left = insertIntoBST(root.left, val);
18+
if (root == null) {
19+
return new TreeNode(val);
1920
}
20-
else if(val > root.val){
21+
if (root.val < val) {
2122
root.right = insertIntoBST(root.right, val);
23+
} else {
24+
root.left = insertIntoBST(root.left, val);
2225
}
23-
24-
// return the unchanged pointer
2526
return root;
2627
}
27-
}
28+
}
Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,18 @@
11
# Definition for a binary tree node.
22
# class TreeNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.left = None
6-
# self.right = None
7-
8-
# 解法一
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
97
class Solution:
10-
def insertIntoBST(self, root, val):
11-
"""
12-
:type root: TreeNode
13-
:type val: int
14-
:rtype: TreeNode
15-
"""
16-
if not root:
17-
return TreeNode(val)
18-
if root.val < val:
19-
if root.right:
20-
self.insertIntoBST(root.right, val)
21-
else:
22-
root.right = TreeNode(val)
23-
if root.val > val:
24-
if root.left:
25-
self.insertIntoBST(root.left, val)
8+
def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode:
9+
def dfs(root):
10+
if root is None:
11+
return TreeNode(val)
12+
if root.val < val:
13+
root.right = dfs(root.right)
2614
else:
27-
root.left = TreeNode(val)
28-
return root
15+
root.left = dfs(root.left)
16+
return root
2917

30-
31-
"""
32-
# 解法二
33-
class Solution:
34-
def insertIntoBST(self, root, val):
35-
"""
36-
: type root: TreeNode
37-
: type val: int
38-
: rtype: TreeNode
39-
"""
40-
if not root:
41-
return TreeNode(val)
42-
elif root.left is None:
43-
root.left = TreeNode(root.val)
44-
root.val = val
45-
root.val, val = val, root.val
46-
node = root.left
47-
while node.right:
48-
node = node.right
49-
node.right = TreeNode(val)
50-
return root
51-
"""
18+
return dfs(root)

0 commit comments

Comments
 (0)