Skip to content

Commit 4571842

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

File tree

4 files changed

+200
-0
lines changed

4 files changed

+200
-0
lines changed

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

+70
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,76 @@ func insertIntoMaxTree(root *TreeNode, val int) *TreeNode {
348348
}
349349
```
350350

351+
### **C**
352+
353+
```c
354+
/**
355+
* Definition for a binary tree node.
356+
* struct TreeNode {
357+
* int val;
358+
* struct TreeNode *left;
359+
* struct TreeNode *right;
360+
* };
361+
*/
362+
363+
364+
struct TreeNode *insertIntoMaxTree(struct TreeNode *root, int val) {
365+
if (!root || root->val < val) {
366+
struct TreeNode *res = (struct TreeNode *) malloc(sizeof(struct TreeNode));
367+
res->val = val;
368+
res->left = root;
369+
res->right = NULL;
370+
return res;
371+
}
372+
root->right = insertIntoMaxTree(root->right, val);
373+
return root;
374+
}
375+
```
376+
377+
### **Rust**
378+
379+
```rust
380+
// Definition for a binary tree node.
381+
// #[derive(Debug, PartialEq, Eq)]
382+
// pub struct TreeNode {
383+
// pub val: i32,
384+
// pub left: Option<Rc<RefCell<TreeNode>>>,
385+
// pub right: Option<Rc<RefCell<TreeNode>>>,
386+
// }
387+
//
388+
// impl TreeNode {
389+
// #[inline]
390+
// pub fn new(val: i32) -> Self {
391+
// TreeNode {
392+
// val,
393+
// left: None,
394+
// right: None
395+
// }
396+
// }
397+
// }
398+
use std::rc::Rc;
399+
use std::cell::RefCell;
400+
impl Solution {
401+
pub fn insert_into_max_tree(
402+
mut root: Option<Rc<RefCell<TreeNode>>>,
403+
val: i32,
404+
) -> Option<Rc<RefCell<TreeNode>>> {
405+
if root.is_none() || root.as_ref().unwrap().as_ref().borrow().val < val {
406+
return Some(Rc::new(RefCell::new(TreeNode {
407+
val,
408+
left: root.take(),
409+
right: None,
410+
})));
411+
}
412+
{
413+
let mut root = root.as_ref().unwrap().as_ref().borrow_mut();
414+
root.right = Self::insert_into_max_tree(root.right.take(), val);
415+
}
416+
root
417+
}
418+
}
419+
```
420+
351421
### **...**
352422

353423
```

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

+70
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,76 @@ func insertIntoMaxTree(root *TreeNode, val int) *TreeNode {
318318
}
319319
```
320320

321+
### **C**
322+
323+
```c
324+
/**
325+
* Definition for a binary tree node.
326+
* struct TreeNode {
327+
* int val;
328+
* struct TreeNode *left;
329+
* struct TreeNode *right;
330+
* };
331+
*/
332+
333+
334+
struct TreeNode *insertIntoMaxTree(struct TreeNode *root, int val) {
335+
if (!root || root->val < val) {
336+
struct TreeNode *res = (struct TreeNode *) malloc(sizeof(struct TreeNode));
337+
res->val = val;
338+
res->left = root;
339+
res->right = NULL;
340+
return res;
341+
}
342+
root->right = insertIntoMaxTree(root->right, val);
343+
return root;
344+
}
345+
```
346+
347+
### **Rust**
348+
349+
```rust
350+
// Definition for a binary tree node.
351+
// #[derive(Debug, PartialEq, Eq)]
352+
// pub struct TreeNode {
353+
// pub val: i32,
354+
// pub left: Option<Rc<RefCell<TreeNode>>>,
355+
// pub right: Option<Rc<RefCell<TreeNode>>>,
356+
// }
357+
//
358+
// impl TreeNode {
359+
// #[inline]
360+
// pub fn new(val: i32) -> Self {
361+
// TreeNode {
362+
// val,
363+
// left: None,
364+
// right: None
365+
// }
366+
// }
367+
// }
368+
use std::rc::Rc;
369+
use std::cell::RefCell;
370+
impl Solution {
371+
pub fn insert_into_max_tree(
372+
mut root: Option<Rc<RefCell<TreeNode>>>,
373+
val: i32,
374+
) -> Option<Rc<RefCell<TreeNode>>> {
375+
if root.is_none() || root.as_ref().unwrap().as_ref().borrow().val < val {
376+
return Some(Rc::new(RefCell::new(TreeNode {
377+
val,
378+
left: root.take(),
379+
right: None,
380+
})));
381+
}
382+
{
383+
let mut root = root.as_ref().unwrap().as_ref().borrow_mut();
384+
root.right = Self::insert_into_max_tree(root.right.take(), val);
385+
}
386+
root
387+
}
388+
}
389+
```
390+
321391
### **...**
322392

323393
```
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+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
11+
struct TreeNode *insertIntoMaxTree(struct TreeNode *root, int val) {
12+
if (!root || root->val < val) {
13+
struct TreeNode *res = (struct TreeNode *) malloc(sizeof(struct TreeNode));
14+
res->val = val;
15+
res->left = root;
16+
res->right = NULL;
17+
return res;
18+
}
19+
root->right = insertIntoMaxTree(root->right, val);
20+
return root;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
pub fn insert_into_max_tree(
23+
mut root: Option<Rc<RefCell<TreeNode>>>,
24+
val: i32,
25+
) -> Option<Rc<RefCell<TreeNode>>> {
26+
if root.is_none() || root.as_ref().unwrap().as_ref().borrow().val < val {
27+
return Some(Rc::new(RefCell::new(TreeNode {
28+
val,
29+
left: root.take(),
30+
right: None,
31+
})));
32+
}
33+
{
34+
let mut root = root.as_ref().unwrap().as_ref().borrow_mut();
35+
root.right = Self::insert_into_max_tree(root.right.take(), val);
36+
}
37+
root
38+
}
39+
}

0 commit comments

Comments
 (0)