Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 36b3ef4

Browse files
committed
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‎

Lines changed: 154 additions & 14 deletions
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
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /