Skip to content

Commit c8fea0e

Browse files
committed
feat: add solutions to lc problems: No.0654,1295
1 parent e2d8ac6 commit c8fea0e

File tree

12 files changed

+440
-27
lines changed

12 files changed

+440
-27
lines changed

solution/0600-0699/0654.Maximum Binary Tree/README.md

+119-2
Original file line numberDiff line numberDiff line change
@@ -52,27 +52,144 @@
5252
<li><code>nums</code> 中的所有整数 <strong>互不相同</strong></li>
5353
</ul>
5454

55-
5655
## 解法
5756

5857
<!-- 这里可写通用的实现逻辑 -->
5958

59+
先找到数组的最大元素所在的位置,作为根节点,然后递归左右两侧的子数组,构建左右子树。
60+
6061
<!-- tabs:start -->
6162

6263
### **Python3**
6364

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

6667
```python
67-
68+
# Definition for a binary tree node.
69+
# class TreeNode:
70+
# def __init__(self, val=0, left=None, right=None):
71+
# self.val = val
72+
# self.left = left
73+
# self.right = right
74+
class Solution:
75+
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
76+
def inner(nums, l, r):
77+
if l > r:
78+
return None
79+
mx = l
80+
for i in range(l + 1, r + 1):
81+
if nums[mx] < nums[i]:
82+
mx = i
83+
return TreeNode(nums[mx], inner(nums, l, mx - 1), inner(nums, mx + 1, r))
84+
85+
return inner(nums, 0, len(nums) - 1)
6886
```
6987

7088
### **Java**
7189

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

7492
```java
93+
/**
94+
* Definition for a binary tree node.
95+
* public class TreeNode {
96+
* int val;
97+
* TreeNode left;
98+
* TreeNode right;
99+
* TreeNode() {}
100+
* TreeNode(int val) { this.val = val; }
101+
* TreeNode(int val, TreeNode left, TreeNode right) {
102+
* this.val = val;
103+
* this.left = left;
104+
* this.right = right;
105+
* }
106+
* }
107+
*/
108+
class Solution {
109+
public TreeNode constructMaximumBinaryTree(int[] nums) {
110+
return construct(nums, 0, nums.length - 1);
111+
}
112+
113+
private TreeNode construct(int[] nums, int l, int r) {
114+
if (l > r) {
115+
return null;
116+
}
117+
int mx = l;
118+
for (int i = l + 1; i <= r; ++i) {
119+
if (nums[mx] < nums[i]) {
120+
mx = i;
121+
}
122+
}
123+
return new TreeNode(nums[mx], construct(nums, l, mx - 1), construct(nums, mx + 1, r));
124+
}
125+
}
126+
```
127+
128+
### **C++**
129+
130+
```cpp
131+
/**
132+
* Definition for a binary tree node.
133+
* struct TreeNode {
134+
* int val;
135+
* TreeNode *left;
136+
* TreeNode *right;
137+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
138+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
139+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
140+
* };
141+
*/
142+
class Solution {
143+
public:
144+
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
145+
return construct(nums, 0, nums.size() - 1);
146+
}
147+
148+
TreeNode* construct(vector<int>& nums, int l, int r) {
149+
if (l > r) return nullptr;
150+
int mx = l;
151+
for (int i = l + 1; i <= r; ++i) {
152+
if (nums[mx] < nums[i]) mx = i;
153+
}
154+
TreeNode* root = new TreeNode(nums[mx]);
155+
root->left = construct(nums, l, mx - 1);
156+
root->right = construct(nums, mx + 1, r);
157+
return root;
158+
}
159+
};
160+
```
75161

162+
### **Go**
163+
164+
```go
165+
/**
166+
* Definition for a binary tree node.
167+
* type TreeNode struct {
168+
* Val int
169+
* Left *TreeNode
170+
* Right *TreeNode
171+
* }
172+
*/
173+
func constructMaximumBinaryTree(nums []int) *TreeNode {
174+
return construct(nums, 0, len(nums)-1)
175+
}
176+
177+
func construct(nums []int, l, r int) *TreeNode {
178+
if l > r {
179+
return nil
180+
}
181+
mx := l
182+
for i := l + 1; i <= r; i++ {
183+
if nums[mx] < nums[i] {
184+
mx = i
185+
}
186+
}
187+
return &TreeNode{
188+
Val: nums[mx],
189+
Left: construct(nums, l, mx-1),
190+
Right: construct(nums, mx+1, r),
191+
}
192+
}
76193
```
77194

78195
### **...**

solution/0600-0699/0654.Maximum Binary Tree/README_EN.md

+117-2
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,136 @@
4848
<li>All integers in <code>nums</code> are <strong>unique</strong>.</li>
4949
</ul>
5050

51-
5251
## Solutions
5352

5453
<!-- tabs:start -->
5554

5655
### **Python3**
5756

5857
```python
59-
58+
# Definition for a binary tree node.
59+
# class TreeNode:
60+
# def __init__(self, val=0, left=None, right=None):
61+
# self.val = val
62+
# self.left = left
63+
# self.right = right
64+
class Solution:
65+
def constructMaximumBinaryTree(self, nums: List[int]) -> TreeNode:
66+
def inner(nums, l, r):
67+
if l > r:
68+
return None
69+
mx = l
70+
for i in range(l + 1, r + 1):
71+
if nums[mx] < nums[i]:
72+
mx = i
73+
return TreeNode(nums[mx], inner(nums, l, mx - 1), inner(nums, mx + 1, r))
74+
75+
return inner(nums, 0, len(nums) - 1)
6076
```
6177

6278
### **Java**
6379

6480
```java
81+
/**
82+
* Definition for a binary tree node.
83+
* public class TreeNode {
84+
* int val;
85+
* TreeNode left;
86+
* TreeNode right;
87+
* TreeNode() {}
88+
* TreeNode(int val) { this.val = val; }
89+
* TreeNode(int val, TreeNode left, TreeNode right) {
90+
* this.val = val;
91+
* this.left = left;
92+
* this.right = right;
93+
* }
94+
* }
95+
*/
96+
class Solution {
97+
public TreeNode constructMaximumBinaryTree(int[] nums) {
98+
return construct(nums, 0, nums.length - 1);
99+
}
100+
101+
private TreeNode construct(int[] nums, int l, int r) {
102+
if (l > r) {
103+
return null;
104+
}
105+
int mx = l;
106+
for (int i = l + 1; i <= r; ++i) {
107+
if (nums[mx] < nums[i]) {
108+
mx = i;
109+
}
110+
}
111+
return new TreeNode(nums[mx], construct(nums, l, mx - 1), construct(nums, mx + 1, r));
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
/**
120+
* Definition for a binary tree node.
121+
* struct TreeNode {
122+
* int val;
123+
* TreeNode *left;
124+
* TreeNode *right;
125+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
126+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
127+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
128+
* };
129+
*/
130+
class Solution {
131+
public:
132+
TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
133+
return construct(nums, 0, nums.size() - 1);
134+
}
135+
136+
TreeNode* construct(vector<int>& nums, int l, int r) {
137+
if (l > r) return nullptr;
138+
int mx = l;
139+
for (int i = l + 1; i <= r; ++i) {
140+
if (nums[mx] < nums[i]) mx = i;
141+
}
142+
TreeNode* root = new TreeNode(nums[mx]);
143+
root->left = construct(nums, l, mx - 1);
144+
root->right = construct(nums, mx + 1, r);
145+
return root;
146+
}
147+
};
148+
```
65149

150+
### **Go**
151+
152+
```go
153+
/**
154+
* Definition for a binary tree node.
155+
* type TreeNode struct {
156+
* Val int
157+
* Left *TreeNode
158+
* Right *TreeNode
159+
* }
160+
*/
161+
func constructMaximumBinaryTree(nums []int) *TreeNode {
162+
return construct(nums, 0, len(nums)-1)
163+
}
164+
165+
func construct(nums []int, l, r int) *TreeNode {
166+
if l > r {
167+
return nil
168+
}
169+
mx := l
170+
for i := l + 1; i <= r; i++ {
171+
if nums[mx] < nums[i] {
172+
mx = i
173+
}
174+
}
175+
return &TreeNode{
176+
Val: nums[mx],
177+
Left: construct(nums, l, mx-1),
178+
Right: construct(nums, mx+1, r),
179+
}
180+
}
66181
```
67182

68183
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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* constructMaximumBinaryTree(vector<int>& nums) {
15+
return construct(nums, 0, nums.size() - 1);
16+
}
17+
18+
TreeNode* construct(vector<int>& nums, int l, int r) {
19+
if (l > r) return nullptr;
20+
int mx = l;
21+
for (int i = l + 1; i <= r; ++i) {
22+
if (nums[mx] < nums[i]) mx = i;
23+
}
24+
TreeNode* root = new TreeNode(nums[mx]);
25+
root->left = construct(nums, l, mx - 1);
26+
root->right = construct(nums, mx + 1, r);
27+
return root;
28+
}
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 constructMaximumBinaryTree(nums []int) *TreeNode {
10+
return construct(nums, 0, len(nums)-1)
11+
}
12+
13+
func construct(nums []int, l, r int) *TreeNode {
14+
if l > r {
15+
return nil
16+
}
17+
mx := l
18+
for i := l + 1; i <= r; i++ {
19+
if nums[mx] < nums[i] {
20+
mx = i
21+
}
22+
}
23+
return &TreeNode{
24+
Val: nums[mx],
25+
Left: construct(nums, l, mx-1),
26+
Right: construct(nums, mx+1, r),
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 constructMaximumBinaryTree(int[] nums) {
18+
return construct(nums, 0, nums.length - 1);
19+
}
20+
21+
private TreeNode construct(int[] nums, int l, int r) {
22+
if (l > r) {
23+
return null;
24+
}
25+
int mx = l;
26+
for (int i = l + 1; i <= r; ++i) {
27+
if (nums[mx] < nums[i]) {
28+
mx = i;
29+
}
30+
}
31+
return new TreeNode(nums[mx], construct(nums, l, mx - 1), construct(nums, mx + 1, r));
32+
}
33+
}

0 commit comments

Comments
 (0)