71
71
72
72
<!-- 这里可写通用的实现逻辑 -->
73
73
74
- 已知最大树 A,插入一个值 val 后,返回插入后的树。
74
+ ** 方法一:递归 **
75
75
76
- 如果 val 是最大数,那么将 val 作为新的根节点,root 作为新的根节点的左子树。
76
+ 如果 $ val$ 是最大数,那么将 $ val$ 作为新的根节点,$ root$ 作为新的根节点的左子树。
77
77
78
- 如果 val 不是最大数,由于 val 是在最后追加的数,那么一定是在 root 的右边,所以将 val 作为新节点插入 root 的右子树即可。
78
+ 如果 $val$ 不是最大数,由于 $val$ 是在最后追加的数,那么一定是在 $root$ 的右边,所以将 $val$ 作为新节点插入 $root$ 的右子树即可。
79
+
80
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。
81
+
82
+ ** 方法二:迭代**
83
+
84
+ 搜索右子树,找到 $curr.val > val > curr.right.val$ 的节点,然后创建新的节点 $node$,把 $node.left$ 指向 $curr.right$,然后 $curr.right$ 指向 $node$。
85
+
86
+ 最后返回 $root$。
87
+
88
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。
79
89
80
90
<!-- tabs:start -->
81
91
91
101
# self.left = left
92
102
# self.right = right
93
103
class Solution :
94
- def insertIntoMaxTree (self , root : TreeNode, val : int ) -> TreeNode:
104
+ def insertIntoMaxTree (self , root : Optional[ TreeNode] , val : int ) -> Optional[ TreeNode] :
95
105
if root is None or root.val < val:
96
- return TreeNode(val, root, None )
106
+ return TreeNode(val, root)
97
107
root.right = self .insertIntoMaxTree(root.right, val)
98
108
return root
99
109
```
100
110
111
+ ``` python
112
+ # Definition for a binary tree node.
113
+ # class TreeNode:
114
+ # def __init__(self, val=0, left=None, right=None):
115
+ # self.val = val
116
+ # self.left = left
117
+ # self.right = right
118
+ class Solution :
119
+ def insertIntoMaxTree (self , root : Optional[TreeNode], val : int ) -> Optional[TreeNode]:
120
+ if root.val < val:
121
+ return TreeNode(val, root)
122
+ curr = root
123
+ node = TreeNode(val)
124
+ while curr.right and curr.right.val > val:
125
+ curr = curr.right
126
+ node.left = curr.right
127
+ curr.right = node
128
+ return root
129
+ ```
130
+
101
131
### ** Java**
102
132
103
133
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -129,6 +159,39 @@ class Solution {
129
159
}
130
160
```
131
161
162
+ ``` java
163
+ /**
164
+ * Definition for a binary tree node.
165
+ * public class TreeNode {
166
+ * int val;
167
+ * TreeNode left;
168
+ * TreeNode right;
169
+ * TreeNode() {}
170
+ * TreeNode(int val) { this.val = val; }
171
+ * TreeNode(int val, TreeNode left, TreeNode right) {
172
+ * this.val = val;
173
+ * this.left = left;
174
+ * this.right = right;
175
+ * }
176
+ * }
177
+ */
178
+ class Solution {
179
+ public TreeNode insertIntoMaxTree (TreeNode root , int val ) {
180
+ if (root. val < val) {
181
+ return new TreeNode (val, root, null );
182
+ }
183
+ TreeNode curr = root;
184
+ TreeNode node = new TreeNode (val);
185
+ while (curr. right != null && curr. right. val > val) {
186
+ curr = curr. right;
187
+ }
188
+ node. left = curr. right;
189
+ curr. right = node;
190
+ return root;
191
+ }
192
+ }
193
+ ```
194
+
132
195
### ** TypeScript**
133
196
134
197
``` ts
@@ -150,14 +213,47 @@ function insertIntoMaxTree(
150
213
root : TreeNode | null ,
151
214
val : number ,
152
215
): TreeNode | null {
153
- if (root == null || val > root . val ) {
216
+ if (! root || root . val < val ) {
154
217
return new TreeNode (val , root );
155
218
}
156
219
root .right = insertIntoMaxTree (root .right , val );
157
220
return root ;
158
221
}
159
222
```
160
223
224
+ ``` ts
225
+ /**
226
+ * Definition for a binary tree node.
227
+ * class TreeNode {
228
+ * val: number
229
+ * left: TreeNode | null
230
+ * right: TreeNode | null
231
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
232
+ * this.val = (val===undefined ? 0 : val)
233
+ * this.left = (left===undefined ? null : left)
234
+ * this.right = (right===undefined ? null : right)
235
+ * }
236
+ * }
237
+ */
238
+
239
+ function insertIntoMaxTree(
240
+ root : TreeNode | null ,
241
+ val : number ,
242
+ ): TreeNode | null {
243
+ if (root .val < val ) {
244
+ return new TreeNode (val , root );
245
+ }
246
+ const node = new TreeNode (val );
247
+ let curr = root ;
248
+ while (curr .right && curr .right .val > val ) {
249
+ curr = curr .right ;
250
+ }
251
+ node .left = curr .right ;
252
+ curr .right = node ;
253
+ return root ;
254
+ }
255
+ ```
256
+
161
257
### ** C++**
162
258
163
259
``` cpp
@@ -175,15 +271,39 @@ function insertIntoMaxTree(
175
271
class Solution {
176
272
public:
177
273
TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
178
- if (root == nullptr || root->val < val) {
179
- return new TreeNode(val, root, nullptr);
180
- }
274
+ if (!root || root->val < val) return new TreeNode(val, root, nullptr);
181
275
root->right = insertIntoMaxTree(root->right, val);
182
276
return root;
183
277
}
184
278
};
185
279
```
186
280
281
+ ```cpp
282
+ /**
283
+ * Definition for a binary tree node.
284
+ * struct TreeNode {
285
+ * int val;
286
+ * TreeNode *left;
287
+ * TreeNode *right;
288
+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
289
+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
290
+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
291
+ * };
292
+ */
293
+ class Solution {
294
+ public:
295
+ TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
296
+ if (root->val < val) return new TreeNode(val, root, nullptr);
297
+ TreeNode* curr = root;
298
+ TreeNode* node = new TreeNode(val);
299
+ while (curr->right && curr->right->val > val) curr = curr->right;
300
+ node->left = curr->right;
301
+ curr->right = node;
302
+ return root;
303
+ }
304
+ };
305
+ ```
306
+
187
307
### ** Go**
188
308
189
309
``` go
@@ -197,17 +317,37 @@ public:
197
317
*/
198
318
func insertIntoMaxTree (root *TreeNode , val int ) *TreeNode {
199
319
if root == nil || root.Val < val {
200
- return &TreeNode{
201
- Val: val,
202
- Left: root,
203
- Right: nil,
204
- }
320
+ return &TreeNode{val, root, nil }
205
321
}
206
322
root.Right = insertIntoMaxTree (root.Right , val)
207
323
return root
208
324
}
209
325
```
210
326
327
+ ``` go
328
+ /* *
329
+ * Definition for a binary tree node.
330
+ * type TreeNode struct {
331
+ * Val int
332
+ * Left *TreeNode
333
+ * Right *TreeNode
334
+ * }
335
+ */
336
+ func insertIntoMaxTree (root *TreeNode , val int ) *TreeNode {
337
+ if root.Val < val {
338
+ return &TreeNode{val, root, nil }
339
+ }
340
+ node := &TreeNode{Val: val}
341
+ curr := root
342
+ for curr.Right != nil && curr.Right .Val > val {
343
+ curr = curr.Right
344
+ }
345
+ node.Left = curr.Right
346
+ curr.Right = node
347
+ return root
348
+ }
349
+ ```
350
+
211
351
### ** ...**
212
352
213
353
```
0 commit comments