Skip to content

Commit b699629

Browse files
committed
feat: add solutions to lc problem: No.0654
No.0654.Maximum Binary Tree
1 parent 6f55238 commit b699629

File tree

5 files changed

+333
-0
lines changed

5 files changed

+333
-0
lines changed

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

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,122 @@ func construct(nums []int, l, r int) *TreeNode {
192192
}
193193
```
194194

195+
### **C**
196+
197+
```c
198+
/**
199+
* Definition for a binary tree node.
200+
* struct TreeNode {
201+
* int val;
202+
* struct TreeNode *left;
203+
* struct TreeNode *right;
204+
* };
205+
*/
206+
207+
struct TreeNode* construct(int* nums, int start, int end) {
208+
if (start >= end) {
209+
return NULL;
210+
}
211+
int idx = 0;
212+
int maxVal = -1;
213+
for (int i = start; i < end; i++) {
214+
if (nums[i] > maxVal) {
215+
idx = i;
216+
maxVal = nums[i];
217+
}
218+
}
219+
struct TreeNode* res = (struct TreeNode*)malloc(sizeof(struct TreeNode));
220+
res->val = maxVal;
221+
res->left = construct(nums, start, idx);
222+
res->right = construct(nums, idx + 1, end);
223+
return res;
224+
}
225+
226+
struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize) {
227+
return construct(nums, 0, numsSize);
228+
}
229+
```
230+
231+
### **TypeScript**
232+
233+
```ts
234+
/**
235+
* Definition for a binary tree node.
236+
* class TreeNode {
237+
* val: number
238+
* left: TreeNode | null
239+
* right: TreeNode | null
240+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
241+
* this.val = (val===undefined ? 0 : val)
242+
* this.left = (left===undefined ? null : left)
243+
* this.right = (right===undefined ? null : right)
244+
* }
245+
* }
246+
*/
247+
248+
function constructMaximumBinaryTree(nums: number[]): TreeNode | null {
249+
const n = nums.length;
250+
if (n === 0) {
251+
return null;
252+
}
253+
const [val, i] = nums.reduce((r, v, i) => (r[0] < v ? [v, i] : r), [-1, 0]);
254+
return new TreeNode(
255+
val,
256+
constructMaximumBinaryTree(nums.slice(0, i)),
257+
constructMaximumBinaryTree(nums.slice(i + 1)),
258+
);
259+
}
260+
```
261+
262+
### **Rust**
263+
264+
```rust
265+
// Definition for a binary tree node.
266+
// #[derive(Debug, PartialEq, Eq)]
267+
// pub struct TreeNode {
268+
// pub val: i32,
269+
// pub left: Option<Rc<RefCell<TreeNode>>>,
270+
// pub right: Option<Rc<RefCell<TreeNode>>>,
271+
// }
272+
//
273+
// impl TreeNode {
274+
// #[inline]
275+
// pub fn new(val: i32) -> Self {
276+
// TreeNode {
277+
// val,
278+
// left: None,
279+
// right: None
280+
// }
281+
// }
282+
// }
283+
use std::rc::Rc;
284+
use std::cell::RefCell;
285+
impl Solution {
286+
fn construct(nums: &Vec<i32>, start: usize, end: usize) -> Option<Rc<RefCell<TreeNode>>> {
287+
if start >= end {
288+
return None;
289+
}
290+
let mut idx = 0;
291+
let mut max_val = -1;
292+
for i in start..end {
293+
if nums[i] > max_val {
294+
idx = i;
295+
max_val = nums[i];
296+
}
297+
}
298+
Some(Rc::new(RefCell::new(TreeNode {
299+
val: max_val,
300+
left: Self::construct(nums, start, idx),
301+
right: Self::construct(nums, idx + 1, end),
302+
})))
303+
}
304+
305+
pub fn construct_maximum_binary_tree(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
306+
Self::construct(&nums, 0, nums.len())
307+
}
308+
}
309+
```
310+
195311
### **...**
196312

197313
```

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

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,122 @@ func construct(nums []int, l, r int) *TreeNode {
180180
}
181181
```
182182

183+
### **C**
184+
185+
```c
186+
/**
187+
* Definition for a binary tree node.
188+
* struct TreeNode {
189+
* int val;
190+
* struct TreeNode *left;
191+
* struct TreeNode *right;
192+
* };
193+
*/
194+
195+
struct TreeNode* construct(int* nums, int start, int end) {
196+
if (start >= end) {
197+
return NULL;
198+
}
199+
int idx = 0;
200+
int maxVal = -1;
201+
for (int i = start; i < end; i++) {
202+
if (nums[i] > maxVal) {
203+
idx = i;
204+
maxVal = nums[i];
205+
}
206+
}
207+
struct TreeNode* res = (struct TreeNode*)malloc(sizeof(struct TreeNode));
208+
res->val = maxVal;
209+
res->left = construct(nums, start, idx);
210+
res->right = construct(nums, idx + 1, end);
211+
return res;
212+
}
213+
214+
struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize) {
215+
return construct(nums, 0, numsSize);
216+
}
217+
```
218+
219+
### **TypeScript**
220+
221+
```ts
222+
/**
223+
* Definition for a binary tree node.
224+
* class TreeNode {
225+
* val: number
226+
* left: TreeNode | null
227+
* right: TreeNode | null
228+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
229+
* this.val = (val===undefined ? 0 : val)
230+
* this.left = (left===undefined ? null : left)
231+
* this.right = (right===undefined ? null : right)
232+
* }
233+
* }
234+
*/
235+
236+
function constructMaximumBinaryTree(nums: number[]): TreeNode | null {
237+
const n = nums.length;
238+
if (n === 0) {
239+
return null;
240+
}
241+
const [val, i] = nums.reduce((r, v, i) => (r[0] < v ? [v, i] : r), [-1, 0]);
242+
return new TreeNode(
243+
val,
244+
constructMaximumBinaryTree(nums.slice(0, i)),
245+
constructMaximumBinaryTree(nums.slice(i + 1)),
246+
);
247+
}
248+
```
249+
250+
### **Rust**
251+
252+
```rust
253+
// Definition for a binary tree node.
254+
// #[derive(Debug, PartialEq, Eq)]
255+
// pub struct TreeNode {
256+
// pub val: i32,
257+
// pub left: Option<Rc<RefCell<TreeNode>>>,
258+
// pub right: Option<Rc<RefCell<TreeNode>>>,
259+
// }
260+
//
261+
// impl TreeNode {
262+
// #[inline]
263+
// pub fn new(val: i32) -> Self {
264+
// TreeNode {
265+
// val,
266+
// left: None,
267+
// right: None
268+
// }
269+
// }
270+
// }
271+
use std::rc::Rc;
272+
use std::cell::RefCell;
273+
impl Solution {
274+
fn construct(nums: &Vec<i32>, start: usize, end: usize) -> Option<Rc<RefCell<TreeNode>>> {
275+
if start >= end {
276+
return None;
277+
}
278+
let mut idx = 0;
279+
let mut max_val = -1;
280+
for i in start..end {
281+
if nums[i] > max_val {
282+
idx = i;
283+
max_val = nums[i];
284+
}
285+
}
286+
Some(Rc::new(RefCell::new(TreeNode {
287+
val: max_val,
288+
left: Self::construct(nums, start, idx),
289+
right: Self::construct(nums, idx + 1, end),
290+
})))
291+
}
292+
293+
pub fn construct_maximum_binary_tree(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
294+
Self::construct(&nums, 0, nums.len())
295+
}
296+
}
297+
```
298+
183299
### **...**
184300

185301
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
struct TreeNode* construct(int* nums, int start, int end) {
11+
if (start >= end) {
12+
return NULL;
13+
}
14+
int idx = 0;
15+
int maxVal = -1;
16+
for (int i = start; i < end; i++) {
17+
if (nums[i] > maxVal) {
18+
idx = i;
19+
maxVal = nums[i];
20+
}
21+
}
22+
struct TreeNode* res = (struct TreeNode*)malloc(sizeof(struct TreeNode));
23+
res->val = maxVal;
24+
res->left = construct(nums, start, idx);
25+
res->right = construct(nums, idx + 1, end);
26+
return res;
27+
}
28+
29+
struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize) {
30+
return construct(nums, 0, numsSize);
31+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
fn construct(nums: &Vec<i32>, start: usize, end: usize) -> Option<Rc<RefCell<TreeNode>>> {
23+
if start >= end {
24+
return None;
25+
}
26+
let mut idx = 0;
27+
let mut max_val = -1;
28+
for i in start..end {
29+
if nums[i] > max_val {
30+
idx = i;
31+
max_val = nums[i];
32+
}
33+
}
34+
Some(Rc::new(RefCell::new(TreeNode {
35+
val: max_val,
36+
left: Self::construct(nums, start, idx),
37+
right: Self::construct(nums, idx + 1, end),
38+
})))
39+
}
40+
41+
pub fn construct_maximum_binary_tree(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
42+
Self::construct(&nums, 0, nums.len())
43+
}
44+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function constructMaximumBinaryTree(nums: number[]): TreeNode | null {
16+
const n = nums.length;
17+
if (n === 0) {
18+
return null;
19+
}
20+
const [val, i] = nums.reduce((r, v, i) => (r[0] < v ? [v, i] : r), [-1, 0]);
21+
return new TreeNode(
22+
val,
23+
constructMaximumBinaryTree(nums.slice(0, i)),
24+
constructMaximumBinaryTree(nums.slice(i + 1)),
25+
);
26+
}

0 commit comments

Comments
 (0)