Skip to content

Commit f1240f5

Browse files
committed
feat: add solutions to lc problem: No.0998.Maximum Binary Tree II
1 parent c8fea0e commit f1240f5

File tree

6 files changed

+252
-4
lines changed

6 files changed

+252
-4
lines changed

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

+91-2
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,116 @@
6868

6969
<p> </p>
7070

71-
7271
## 解法
7372

7473
<!-- 这里可写通用的实现逻辑 -->
7574

75+
已知最大树 A,插入一个值 val 后,返回插入后的树。
76+
77+
如果 val 是最大数,那么将 val 作为新的根节点,root 作为新的根节点的左子树。
78+
79+
如果 val 不是最大数,由于 val 是在最后追加的数,那么一定是在 root 的右边,所以将 val 作为新节点插入 root 的右子树即可。
80+
7681
<!-- tabs:start -->
7782

7883
### **Python3**
7984

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

8287
```python
83-
88+
# Definition for a binary tree node.
89+
# class TreeNode:
90+
# def __init__(self, val=0, left=None, right=None):
91+
# self.val = val
92+
# self.left = left
93+
# self.right = right
94+
class Solution:
95+
def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
96+
if root is None or root.val < val:
97+
return TreeNode(val, root, None)
98+
root.right = self.insertIntoMaxTree(root.right, val)
99+
return root
84100
```
85101

86102
### **Java**
87103

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

90106
```java
107+
/**
108+
* Definition for a binary tree node.
109+
* public class TreeNode {
110+
* int val;
111+
* TreeNode left;
112+
* TreeNode right;
113+
* TreeNode() {}
114+
* TreeNode(int val) { this.val = val; }
115+
* TreeNode(int val, TreeNode left, TreeNode right) {
116+
* this.val = val;
117+
* this.left = left;
118+
* this.right = right;
119+
* }
120+
* }
121+
*/
122+
class Solution {
123+
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
124+
if (root == null || root.val < val) {
125+
return new TreeNode(val, root, null);
126+
}
127+
root.right = insertIntoMaxTree(root.right, val);
128+
return root;
129+
}
130+
}
131+
```
132+
133+
### **C++**
134+
135+
```cpp
136+
/**
137+
* Definition for a binary tree node.
138+
* struct TreeNode {
139+
* int val;
140+
* TreeNode *left;
141+
* TreeNode *right;
142+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
143+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
144+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
145+
* };
146+
*/
147+
class Solution {
148+
public:
149+
TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
150+
if (root == nullptr || root->val < val) {
151+
return new TreeNode(val, root, nullptr);
152+
}
153+
root->right = insertIntoMaxTree(root->right, val);
154+
return root;
155+
}
156+
};
157+
```
91158
159+
### **Go**
160+
161+
```go
162+
/**
163+
* Definition for a binary tree node.
164+
* type TreeNode struct {
165+
* Val int
166+
* Left *TreeNode
167+
* Right *TreeNode
168+
* }
169+
*/
170+
func insertIntoMaxTree(root *TreeNode, val int) *TreeNode {
171+
if root == nil || root.Val < val {
172+
return &TreeNode{
173+
Val: val,
174+
Left: root,
175+
Right: nil,
176+
}
177+
}
178+
root.Right = insertIntoMaxTree(root.Right, val)
179+
return root
180+
}
92181
```
93182

94183
### **...**

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

+85-2
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,104 @@
6060
<li><code>1 &lt;= B.length &lt;= 100</code></li>
6161
</ul>
6262

63-
6463
## Solutions
6564

6665
<!-- tabs:start -->
6766

6867
### **Python3**
6968

7069
```python
71-
70+
# Definition for a binary tree node.
71+
# class TreeNode:
72+
# def __init__(self, val=0, left=None, right=None):
73+
# self.val = val
74+
# self.left = left
75+
# self.right = right
76+
class Solution:
77+
def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
78+
if root is None or root.val < val:
79+
return TreeNode(val, root, None)
80+
root.right = self.insertIntoMaxTree(root.right, val)
81+
return root
7282
```
7383

7484
### **Java**
7585

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

80163
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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* insertIntoMaxTree(TreeNode* root, int val) {
15+
if (root == nullptr || root->val < val) {
16+
return new TreeNode(val, root, nullptr);
17+
}
18+
root->right = insertIntoMaxTree(root->right, val);
19+
return root;
20+
}
21+
};
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 insertIntoMaxTree(root *TreeNode, val int) *TreeNode {
10+
if root == nil || root.Val < val {
11+
return &TreeNode{
12+
Val: val,
13+
Left: root,
14+
Right: nil,
15+
}
16+
}
17+
root.Right = insertIntoMaxTree(root.Right, val)
18+
return root
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
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+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
18+
if (root == null || root.val < val) {
19+
return new TreeNode(val, root, null);
20+
}
21+
root.right = insertIntoMaxTree(root.right, val);
22+
return root;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode:
9+
if root is None or root.val < val:
10+
return TreeNode(val, root, None)
11+
root.right = self.insertIntoMaxTree(root.right, val)
12+
return root

0 commit comments

Comments
 (0)