Skip to content

Commit 36b3ef4

Browse files
committedAug 20, 2022
feat: add solutions to lc problem: No.0998
No.0998.Maximum Binary Tree II
1 parent f116184 commit 36b3ef4

File tree

6 files changed

+300
-36
lines changed

6 files changed

+300
-36
lines changed
 

‎solution/0900-0999/0998.Maximum Binary Tree II/README.md

+154-14
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,21 @@
7171

7272
<!-- 这里可写通用的实现逻辑 -->
7373

74-
已知最大树 A,插入一个值 val 后,返回插入后的树。
74+
**方法一:递归**
7575

76-
如果 val 是最大数,那么将 val 作为新的根节点,root 作为新的根节点的左子树。
76+
如果 $val$ 是最大数,那么将 $val$ 作为新的根节点,$root$ 作为新的根节点的左子树。
7777

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)$。
7989

8090
<!-- tabs:start -->
8191

@@ -91,13 +101,33 @@
91101
# self.left = left
92102
# self.right = right
93103
class Solution:
94-
def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
104+
def insertIntoMaxTree(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
95105
if root is None or root.val < val:
96-
return TreeNode(val, root, None)
106+
return TreeNode(val, root)
97107
root.right = self.insertIntoMaxTree(root.right, val)
98108
return root
99109
```
100110

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+
101131
### **Java**
102132

103133
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -129,6 +159,39 @@ class Solution {
129159
}
130160
```
131161

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+
132195
### **TypeScript**
133196

134197
```ts
@@ -150,14 +213,47 @@ function insertIntoMaxTree(
150213
root: TreeNode | null,
151214
val: number,
152215
): TreeNode | null {
153-
if (root == null || val > root.val) {
216+
if (!root || root.val < val) {
154217
return new TreeNode(val, root);
155218
}
156219
root.right = insertIntoMaxTree(root.right, val);
157220
return root;
158221
}
159222
```
160223

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+
161257
### **C++**
162258

163259
```cpp
@@ -175,15 +271,39 @@ function insertIntoMaxTree(
175271
class Solution {
176272
public:
177273
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);
181275
root->right = insertIntoMaxTree(root->right, val);
182276
return root;
183277
}
184278
};
185279
```
186280
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+
187307
### **Go**
188308

189309
```go
@@ -197,17 +317,37 @@ public:
197317
*/
198318
func insertIntoMaxTree(root *TreeNode, val int) *TreeNode {
199319
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}
205321
}
206322
root.Right = insertIntoMaxTree(root.Right, val)
207323
return root
208324
}
209325
```
210326

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+
211351
### **...**
212352

213353
```

‎solution/0900-0999/0998.Maximum Binary Tree II/README_EN.md

+141-11
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,33 @@
7373
# self.left = left
7474
# self.right = right
7575
class Solution:
76-
def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
76+
def insertIntoMaxTree(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
7777
if root is None or root.val < val:
78-
return TreeNode(val, root, None)
78+
return TreeNode(val, root)
7979
root.right = self.insertIntoMaxTree(root.right, val)
8080
return root
8181
```
8282

83+
```python
84+
# Definition for a binary tree node.
85+
# class TreeNode:
86+
# def __init__(self, val=0, left=None, right=None):
87+
# self.val = val
88+
# self.left = left
89+
# self.right = right
90+
class Solution:
91+
def insertIntoMaxTree(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
92+
if root.val < val:
93+
return TreeNode(val, root)
94+
curr = root
95+
node = TreeNode(val)
96+
while curr.right and curr.right.val > val:
97+
curr = curr.right
98+
node.left = curr.right
99+
curr.right = node
100+
return root
101+
```
102+
83103
### **Java**
84104

85105
```java
@@ -109,6 +129,39 @@ class Solution {
109129
}
110130
```
111131

132+
```java
133+
/**
134+
* Definition for a binary tree node.
135+
* public class TreeNode {
136+
* int val;
137+
* TreeNode left;
138+
* TreeNode right;
139+
* TreeNode() {}
140+
* TreeNode(int val) { this.val = val; }
141+
* TreeNode(int val, TreeNode left, TreeNode right) {
142+
* this.val = val;
143+
* this.left = left;
144+
* this.right = right;
145+
* }
146+
* }
147+
*/
148+
class Solution {
149+
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
150+
if (root.val < val) {
151+
return new TreeNode(val, root, null);
152+
}
153+
TreeNode curr = root;
154+
TreeNode node = new TreeNode(val);
155+
while (curr.right != null && curr.right.val > val) {
156+
curr = curr.right;
157+
}
158+
node.left = curr.right;
159+
curr.right = node;
160+
return root;
161+
}
162+
}
163+
```
164+
112165
### **TypeScript**
113166

114167
```ts
@@ -130,14 +183,47 @@ function insertIntoMaxTree(
130183
root: TreeNode | null,
131184
val: number,
132185
): TreeNode | null {
133-
if (root == null || val > root.val) {
186+
if (!root || root.val < val) {
134187
return new TreeNode(val, root);
135188
}
136189
root.right = insertIntoMaxTree(root.right, val);
137190
return root;
138191
}
139192
```
140193

194+
```ts
195+
/**
196+
* Definition for a binary tree node.
197+
* class TreeNode {
198+
* val: number
199+
* left: TreeNode | null
200+
* right: TreeNode | null
201+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
202+
* this.val = (val===undefined ? 0 : val)
203+
* this.left = (left===undefined ? null : left)
204+
* this.right = (right===undefined ? null : right)
205+
* }
206+
* }
207+
*/
208+
209+
function insertIntoMaxTree(
210+
root: TreeNode | null,
211+
val: number,
212+
): TreeNode | null {
213+
if (root.val < val) {
214+
return new TreeNode(val, root);
215+
}
216+
const node = new TreeNode(val);
217+
let curr = root;
218+
while (curr.right && curr.right.val > val) {
219+
curr = curr.right;
220+
}
221+
node.left = curr.right;
222+
curr.right = node;
223+
return root;
224+
}
225+
```
226+
141227
### **C++**
142228

143229
```cpp
@@ -155,15 +241,39 @@ function insertIntoMaxTree(
155241
class Solution {
156242
public:
157243
TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
158-
if (root == nullptr || root->val < val) {
159-
return new TreeNode(val, root, nullptr);
160-
}
244+
if (!root || root->val < val) return new TreeNode(val, root, nullptr);
161245
root->right = insertIntoMaxTree(root->right, val);
162246
return root;
163247
}
164248
};
165249
```
166250
251+
```cpp
252+
/**
253+
* Definition for a binary tree node.
254+
* struct TreeNode {
255+
* int val;
256+
* TreeNode *left;
257+
* TreeNode *right;
258+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
259+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
260+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
261+
* };
262+
*/
263+
class Solution {
264+
public:
265+
TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
266+
if (root->val < val) return new TreeNode(val, root, nullptr);
267+
TreeNode* curr = root;
268+
TreeNode* node = new TreeNode(val);
269+
while (curr->right && curr->right->val > val) curr = curr->right;
270+
node->left = curr->right;
271+
curr->right = node;
272+
return root;
273+
}
274+
};
275+
```
276+
167277
### **Go**
168278

169279
```go
@@ -177,17 +287,37 @@ public:
177287
*/
178288
func insertIntoMaxTree(root *TreeNode, val int) *TreeNode {
179289
if root == nil || root.Val < val {
180-
return &TreeNode{
181-
Val: val,
182-
Left: root,
183-
Right: nil,
184-
}
290+
return &TreeNode{val, root, nil}
185291
}
186292
root.Right = insertIntoMaxTree(root.Right, val)
187293
return root
188294
}
189295
```
190296

297+
```go
298+
/**
299+
* Definition for a binary tree node.
300+
* type TreeNode struct {
301+
* Val int
302+
* Left *TreeNode
303+
* Right *TreeNode
304+
* }
305+
*/
306+
func insertIntoMaxTree(root *TreeNode, val int) *TreeNode {
307+
if root.Val < val {
308+
return &TreeNode{val, root, nil}
309+
}
310+
node := &TreeNode{Val: val}
311+
curr := root
312+
for curr.Right != nil && curr.Right.Val > val {
313+
curr = curr.Right
314+
}
315+
node.Left = curr.Right
316+
curr.Right = node
317+
return root
318+
}
319+
```
320+
191321
### **...**
192322

193323
```

‎solution/0900-0999/0998.Maximum Binary Tree II/Solution.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
class Solution {
1313
public:
1414
TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
15-
if (root == nullptr || root->val < val) {
16-
return new TreeNode(val, root, nullptr);
17-
}
15+
if (!root || root->val < val) return new TreeNode(val, root, nullptr);
1816
root->right = insertIntoMaxTree(root->right, val);
1917
return root;
2018
}

‎solution/0900-0999/0998.Maximum Binary Tree II/Solution.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88
*/
99
func insertIntoMaxTree(root *TreeNode, val int) *TreeNode {
1010
if root == nil || root.Val < val {
11-
return &TreeNode{
12-
Val: val,
13-
Left: root,
14-
Right: nil,
15-
}
11+
return &TreeNode{val, root, nil}
1612
}
1713
root.Right = insertIntoMaxTree(root.Right, val)
1814
return root

‎solution/0900-0999/0998.Maximum Binary Tree II/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
# self.left = left
66
# self.right = right
77
class Solution:
8-
def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
8+
def insertIntoMaxTree(self, root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
99
if root is None or root.val < val:
10-
return TreeNode(val, root, None)
10+
return TreeNode(val, root)
1111
root.right = self.insertIntoMaxTree(root.right, val)
1212
return root

‎solution/0900-0999/0998.Maximum Binary Tree II/Solution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function insertIntoMaxTree(
1616
root: TreeNode | null,
1717
val: number,
1818
): TreeNode | null {
19-
if (root == null || val > root.val) {
19+
if (!root || root.val < val) {
2020
return new TreeNode(val, root);
2121
}
2222
root.right = insertIntoMaxTree(root.right, val);

0 commit comments

Comments
 (0)
Please sign in to comment.