Skip to content

Commit f2b019a

Browse files
committed
add 95;104;107;108;110
1 parent 3544617 commit f2b019a

5 files changed

+247
-0
lines changed

104.maximum-depth-of-binary-tree.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @lc app=leetcode id=104 lang=rust
3+
*
4+
* [104] Maximum Depth of Binary Tree
5+
*/
6+
7+
// @lc code=start
8+
// Definition for a binary tree node.
9+
// #[derive(Debug, PartialEq, Eq)]
10+
// pub struct TreeNode {
11+
// pub val: i32,
12+
// pub left: Option<Rc<RefCell<TreeNode>>>,
13+
// pub right: Option<Rc<RefCell<TreeNode>>>,
14+
// }
15+
//
16+
// impl TreeNode {
17+
// #[inline]
18+
// pub fn new(val: i32) -> Self {
19+
// TreeNode {
20+
// val,
21+
// left: None,
22+
// right: None
23+
// }
24+
// }
25+
// }
26+
use std::rc::Rc;
27+
use std::cell::RefCell;
28+
use std::cmp;
29+
30+
impl Solution {
31+
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
32+
match root {
33+
None => 0,
34+
Some(node) => {
35+
1 + cmp::max(
36+
Self::max_depth(node.borrow().left.clone()),
37+
Self::max_depth(node.borrow().right.clone())
38+
)
39+
}
40+
}
41+
}
42+
}
43+
// @lc code=end
44+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @lc app=leetcode id=107 lang=rust
3+
*
4+
* [107] Binary Tree Level Order Traversal II
5+
*/
6+
7+
// @lc code=start
8+
// Definition for a binary tree node.
9+
// #[derive(Debug, PartialEq, Eq)]
10+
// pub struct TreeNode {
11+
// pub val: i32,
12+
// pub left: Option<Rc<RefCell<TreeNode>>>,
13+
// pub right: Option<Rc<RefCell<TreeNode>>>,
14+
// }
15+
//
16+
// impl TreeNode {
17+
// #[inline]
18+
// pub fn new(val: i32) -> Self {
19+
// TreeNode {
20+
// val,
21+
// left: None,
22+
// right: None
23+
// }
24+
// }
25+
// }
26+
use std::rc::Rc;
27+
use std::cell::RefCell;
28+
impl Solution {
29+
pub fn level_order_bottom(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
30+
if root.is_none() { return vec![]; }
31+
32+
let mut nodes = vec![root.clone()];
33+
let mut ans = vec![];
34+
35+
while nodes.len() > 0 {
36+
let mut tmp = vec![];
37+
let mut vals = vec![];
38+
39+
for node in nodes {
40+
if let Some(node_inner) = node {
41+
let tree_node = node_inner.borrow();
42+
vals.push(tree_node.val);
43+
if tree_node.left.is_some() {
44+
tmp.push(tree_node.left.clone());
45+
}
46+
if tree_node.right.is_some() {
47+
tmp.push(tree_node.right.clone());
48+
}
49+
}
50+
}
51+
52+
nodes = tmp;
53+
ans.push(vals);
54+
}
55+
56+
ans.reverse();
57+
ans
58+
}
59+
}
60+
// @lc code=end
61+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @lc app=leetcode id=108 lang=rust
3+
*
4+
* [108] Convert Sorted Array to Binary Search Tree
5+
*/
6+
7+
// @lc code=start
8+
// Definition for a binary tree node.
9+
// #[derive(Debug, PartialEq, Eq)]
10+
// pub struct TreeNode {
11+
// pub val: i32,
12+
// pub left: Option<Rc<RefCell<TreeNode>>>,
13+
// pub right: Option<Rc<RefCell<TreeNode>>>,
14+
// }
15+
//
16+
// impl TreeNode {
17+
// #[inline]
18+
// pub fn new(val: i32) -> Self {
19+
// TreeNode {
20+
// val,
21+
// left: None,
22+
// right: None
23+
// }
24+
// }
25+
// }
26+
use std::rc::Rc;
27+
use std::cell::RefCell;
28+
impl Solution {
29+
pub fn sorted_array_to_bst(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
30+
fn helper(nums: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {
31+
let N = nums.len();
32+
if N == 0 { return None; }
33+
34+
let mid = N / 2;
35+
let mut tree_node = TreeNode::new(nums[mid]);
36+
37+
tree_node.left = helper(&nums[0..mid]);
38+
tree_node.right = helper(&nums[mid+1..N]);
39+
40+
Some(Rc::new(RefCell::new(tree_node)))
41+
}
42+
43+
helper(&nums[..])
44+
}
45+
}
46+
// @lc code=end
47+

110.balanced-binary-tree.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @lc app=leetcode id=110 lang=rust
3+
*
4+
* [110] Balanced Binary Tree
5+
*/
6+
7+
// @lc code=start
8+
// Definition for a binary tree node.
9+
// #[derive(Debug, PartialEq, Eq)]
10+
// pub struct TreeNode {
11+
// pub val: i32,
12+
// pub left: Option<Rc<RefCell<TreeNode>>>,
13+
// pub right: Option<Rc<RefCell<TreeNode>>>,
14+
// }
15+
//
16+
// impl TreeNode {
17+
// #[inline]
18+
// pub fn new(val: i32) -> Self {
19+
// TreeNode {
20+
// val,
21+
// left: None,
22+
// right: None
23+
// }
24+
// }
25+
// }
26+
use std::rc::Rc;
27+
use std::cell::RefCell;
28+
impl Solution {
29+
pub fn is_balanced(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
30+
31+
}
32+
}
33+
// @lc code=end
34+

95.unique-binary-search-trees-ii.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @lc app=leetcode id=95 lang=rust
3+
*
4+
* [95] Unique Binary Search Trees II
5+
*/
6+
7+
// @lc code=start
8+
// Definition for a binary tree node.
9+
// #[derive(Debug, PartialEq, Eq)]
10+
// pub struct TreeNode {
11+
// pub val: i32,
12+
// pub left: Option<Rc<RefCell<TreeNode>>>,
13+
// pub right: Option<Rc<RefCell<TreeNode>>>,
14+
// }
15+
//
16+
// impl TreeNode {
17+
// #[inline]
18+
// pub fn new(val: i32) -> Self {
19+
// TreeNode {
20+
// val,
21+
// left: None,
22+
// right: None
23+
// }
24+
// }
25+
// }
26+
use std::rc::Rc;
27+
use std::cell::RefCell;
28+
impl Solution {
29+
pub fn generate_trees(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
30+
fn helper(start: i32, end: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
31+
let mut ans = vec![];
32+
33+
if start > end {
34+
ans.push(None);
35+
return ans;
36+
}
37+
38+
for i in start..end+1 {
39+
40+
for left in helper(start, i-1) {
41+
for right in helper(i+1, end) {
42+
let tree_node = Rc::new(RefCell::new(TreeNode::new(i)));
43+
tree_node.borrow_mut().left = left.clone();
44+
tree_node.borrow_mut().right = right.clone();
45+
// if let Some(node) = right.clone() {
46+
// println!("i: {}, right: {}", i, node.borrow().val);
47+
// }
48+
ans.push(Some(tree_node));
49+
}
50+
}
51+
}
52+
53+
ans
54+
}
55+
56+
if n == 0 { return vec![]; }
57+
helper(1, n)
58+
}
59+
}
60+
// @lc code=end
61+

0 commit comments

Comments
 (0)