Skip to content

Commit 947a598

Browse files
author
joker53-1
committed
first
1 parent f6dc945 commit 947a598

6 files changed

+363
-4
lines changed

src/problem/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ mod p0102_binary_tree_level_order_traversal;
1818
mod p0103_binary_tree_zigzag_level_order_traversal;
1919
mod p0104_maximum_depth_of_binary_tree;
2020
mod p0105_construct_binary_tree_from_preorder_and_inorder_traversal;
21+
mod p0106_construct_binary_tree_from_inorder_and_postorder_traversal;
22+
mod p0107_binary_tree_level_order_traversal_ii;
23+
mod p0108_convert_sorted_array_to_binary_search_tree;
24+
mod p0109_convert_sorted_list_to_binary_search_tree;

src/problem/p0105_construct_binary_tree_from_preorder_and_inorder_traversal.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ impl Solution {
6161
for (i, v) in inorder.iter().enumerate() {
6262
map.insert(v, i);
6363
}
64-
Self::cur(0, 0, inorder.len() - 1, &preorder, &map)
64+
Self::cur(0, 0, (inorder.len() - 1) as i32, &preorder, &map)
6565
}
6666

67-
fn cur(root: usize, left: usize, right: usize, preorder: &Vec<i32>, map: &HashMap<&i32, usize>)-> Option<Rc<RefCell<TreeNode>>> {
67+
fn cur(root: usize, left: i32, right: i32, preorder: &Vec<i32>, map: &HashMap<&i32, usize>)-> Option<Rc<RefCell<TreeNode>>> {
6868
if left > right { return None; }
6969
let mut node = TreeNode::new(preorder[root]);
7070
let i = map.get(&preorder[root]).unwrap();
71-
node.left = Self::cur(root + 1, left, i - 1, &preorder, map);
72-
node.right = Self::cur(root + i - left + 1, i + 1, right, &preorder, map);
71+
node.left = Self::cur(root + 1, left, (i - 1) as i32, preorder, map);
72+
node.right = Self::cur(root + i - (left as usize) + 1, (i + 1) as i32, right, preorder, map);
7373
Some(Rc::new(RefCell::from(node)))
7474
}
7575
}
@@ -82,5 +82,6 @@ mod tests {
8282

8383
#[test]
8484
fn test_105() {
85+
Solution::build_tree(vec![3,9,20,15,7], vec![9,3,15,20,7]).unwrap();
8586
}
8687
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* [106] Construct Binary Tree from Inorder and Postorder Traversal
3+
*
4+
* Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.
5+
*
6+
* <strong class="example">Example 1:
7+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree.jpg" style="width: 277px; height: 302px;" />
8+
* Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
9+
* Output: [3,9,20,null,null,15,7]
10+
*
11+
* <strong class="example">Example 2:
12+
*
13+
* Input: inorder = [-1], postorder = [-1]
14+
* Output: [-1]
15+
*
16+
*
17+
* Constraints:
18+
*
19+
* 1 <= inorder.length <= 3000
20+
* postorder.length == inorder.length
21+
* -3000 <= inorder[i], postorder[i] <= 3000
22+
* inorder and postorder consist of unique values.
23+
* Each value of postorder also appears in inorder.
24+
* inorder is guaranteed to be the inorder traversal of the tree.
25+
* postorder is guaranteed to be the postorder traversal of the tree.
26+
*
27+
*/
28+
pub struct Solution {}
29+
use crate::util::tree::{TreeNode, to_tree};
30+
31+
// problem: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
32+
// discuss: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/discuss/?currentPage=1&orderBy=most_votes&query=
33+
34+
// submission codes start here
35+
36+
// Definition for a binary tree node.
37+
// #[derive(Debug, PartialEq, Eq)]
38+
// pub struct TreeNode {
39+
// pub val: i32,
40+
// pub left: Option<Rc<RefCell<TreeNode>>>,
41+
// pub right: Option<Rc<RefCell<TreeNode>>>,
42+
// }
43+
//
44+
// impl TreeNode {
45+
// #[inline]
46+
// pub fn new(val: i32) -> Self {
47+
// TreeNode {
48+
// val,
49+
// left: None,
50+
// right: None
51+
// }
52+
// }
53+
// }
54+
use std::rc::Rc;
55+
use std::cell::RefCell;
56+
use std::collections::HashMap;
57+
58+
impl Solution {
59+
pub fn build_tree(inorder: Vec<i32>, postorder: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
60+
let mut map = HashMap::new();
61+
for (i, v) in inorder.iter().enumerate() {
62+
map.insert(v, i);
63+
}
64+
Self::recur((postorder.len() - 1) as i32, 0 , (postorder.len() - 1) as i32, &postorder, &map)
65+
}
66+
67+
fn recur(root: i32, left: i32, right: i32, postorder: &Vec<i32>, map: &HashMap<&i32, usize>) -> Option<Rc<RefCell<TreeNode>>> {
68+
if left > right { return None; }
69+
let mut node = TreeNode::new(postorder[root as usize]);
70+
let i = map.get(&postorder[root as usize]).unwrap();
71+
node.left = Self::recur(root - (right - (*i as i32) + 1), left, (i - 1) as i32, postorder, map );
72+
node.right = Self::recur(root - 1, (i + 1) as i32, right, postorder, map);
73+
Some(Rc::new(RefCell::new(node)))
74+
}
75+
}
76+
77+
// submission codes end
78+
79+
#[cfg(test)]
80+
mod tests {
81+
use super::*;
82+
83+
#[test]
84+
fn test_106() {
85+
}
86+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* [107] Binary Tree Level Order Traversal II
3+
*
4+
* Given the root of a binary tree, return the bottom-up level order traversal of its nodes' values. (i.e., from left to right, level by level from leaf to root).
5+
*
6+
* <strong class="example">Example 1:
7+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg" style="width: 277px; height: 302px;" />
8+
* Input: root = [3,9,20,null,null,15,7]
9+
* Output: [[15,7],[9,20],[3]]
10+
*
11+
* <strong class="example">Example 2:
12+
*
13+
* Input: root = [1]
14+
* Output: [[1]]
15+
*
16+
* <strong class="example">Example 3:
17+
*
18+
* Input: root = []
19+
* Output: []
20+
*
21+
*
22+
* Constraints:
23+
*
24+
* The number of nodes in the tree is in the range [0, 2000].
25+
* -1000 <= Node.val <= 1000
26+
*
27+
*/
28+
pub struct Solution {}
29+
use crate::util::tree::{TreeNode, to_tree};
30+
31+
// problem: https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
32+
// discuss: https://leetcode.com/problems/binary-tree-level-order-traversal-ii/discuss/?currentPage=1&orderBy=most_votes&query=
33+
34+
// submission codes start here
35+
36+
// Definition for a binary tree node.
37+
// #[derive(Debug, PartialEq, Eq)]
38+
// pub struct TreeNode {
39+
// pub val: i32,
40+
// pub left: Option<Rc<RefCell<TreeNode>>>,
41+
// pub right: Option<Rc<RefCell<TreeNode>>>,
42+
// }
43+
//
44+
// impl TreeNode {
45+
// #[inline]
46+
// pub fn new(val: i32) -> Self {
47+
// TreeNode {
48+
// val,
49+
// left: None,
50+
// right: None
51+
// }
52+
// }
53+
// }
54+
use std::rc::Rc;
55+
use std::cell::RefCell;
56+
use std::collections::VecDeque;
57+
58+
impl Solution {
59+
pub fn level_order_bottom(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<i32>> {
60+
if root.is_none() { return vec![]; }
61+
let mut queue = VecDeque::new();
62+
let mut result = vec![];
63+
queue.push_back(root);
64+
while !queue.is_empty() {
65+
let len = queue.len();
66+
let mut level = Vec::with_capacity(len);
67+
for i in 0..len {
68+
let node = queue.pop_front().unwrap();
69+
if let Some(node) = node {
70+
level.push(node.as_ref().borrow().val);
71+
queue.push_back(node.as_ref().borrow().left.clone());
72+
queue.push_back(node.as_ref().borrow().right.clone());
73+
}
74+
}
75+
if level.len() != 0 {
76+
result.push(level);
77+
}
78+
}
79+
result.reverse();
80+
result
81+
}
82+
}
83+
84+
// submission codes end
85+
86+
#[cfg(test)]
87+
mod tests {
88+
use super::*;
89+
90+
#[test]
91+
fn test_107() {
92+
}
93+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* [108] Convert Sorted Array to Binary Search Tree
3+
*
4+
* Given an integer array nums where the elements are sorted in ascending order, convert it to a <span data-keyword="height-balanced">height-balanced</span> binary search tree.
5+
*
6+
* <strong class="example">Example 1:
7+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree1.jpg" style="width: 302px; height: 222px;" />
8+
* Input: nums = [-10,-3,0,5,9]
9+
* Output: [0,-3,9,-10,null,5]
10+
* Explanation: [0,-10,5,null,-3,null,9] is also accepted:
11+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree2.jpg" style="width: 302px; height: 222px;" />
12+
*
13+
* <strong class="example">Example 2:
14+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/btree.jpg" style="width: 342px; height: 142px;" />
15+
* Input: nums = [1,3]
16+
* Output: [3,1]
17+
* Explanation: [1,null,3] and [3,1] are both height-balanced BSTs.
18+
*
19+
*
20+
* Constraints:
21+
*
22+
* 1 <= nums.length <= 10^4
23+
* -10^4 <= nums[i] <= 10^4
24+
* nums is sorted in a strictly increasing order.
25+
*
26+
*/
27+
pub struct Solution {}
28+
use crate::util::tree::{TreeNode, to_tree};
29+
30+
// problem: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
31+
// discuss: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/discuss/?currentPage=1&orderBy=most_votes&query=
32+
33+
// submission codes start here
34+
35+
// Definition for a binary tree node.
36+
// #[derive(Debug, PartialEq, Eq)]
37+
// pub struct TreeNode {
38+
// pub val: i32,
39+
// pub left: Option<Rc<RefCell<TreeNode>>>,
40+
// pub right: Option<Rc<RefCell<TreeNode>>>,
41+
// }
42+
//
43+
// impl TreeNode {
44+
// #[inline]
45+
// pub fn new(val: i32) -> Self {
46+
// TreeNode {
47+
// val,
48+
// left: None,
49+
// right: None
50+
// }
51+
// }
52+
// }
53+
use std::rc::Rc;
54+
use std::cell::RefCell;
55+
impl Solution {
56+
pub fn sorted_array_to_bst(nums: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
57+
if nums.is_empty() {
58+
return None;
59+
}
60+
Self::dfs(&nums, 0, (nums.len() - 1) as i32)
61+
}
62+
63+
fn dfs(nums: &Vec<i32>, l: i32, r: i32) -> Option<Rc<RefCell<TreeNode>>> {
64+
if l > r {
65+
return None;
66+
}
67+
let mid = l + (r - l) / 2;
68+
let mut root = TreeNode::new(nums[mid as usize]);
69+
root.left = Self::dfs(nums, l, mid);
70+
root.right = Self::dfs(nums, mid + 1, r);
71+
Some(Rc::new(RefCell::new(root)))
72+
}
73+
}
74+
75+
// submission codes end
76+
77+
#[cfg(test)]
78+
mod tests {
79+
use super::*;
80+
81+
#[test]
82+
fn test_108() {
83+
}
84+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* [109] Convert Sorted List to Binary Search Tree
3+
*
4+
* Given the head of a singly linked list where elements are sorted in ascending order, convert it to a <span data-keyword="height-balanced">height-balanced</span> binary search tree.
5+
*
6+
* <strong class="example">Example 1:
7+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/08/17/linked.jpg" style="width: 500px; height: 388px;" />
8+
* Input: head = [-10,-3,0,5,9]
9+
* Output: [0,-3,9,-10,null,5]
10+
* Explanation: One possible answer is [0,-3,9,-10,null,5], which represents the shown height balanced BST.
11+
*
12+
* <strong class="example">Example 2:
13+
*
14+
* Input: head = []
15+
* Output: []
16+
*
17+
*
18+
* Constraints:
19+
*
20+
* The number of nodes in head is in the range [0, 2 * 10^4].
21+
* -10^5 <= Node.val <= 10^5
22+
*
23+
*/
24+
pub struct Solution {}
25+
use crate::util::linked_list::{ListNode, to_list};
26+
use crate::util::tree::{TreeNode, to_tree};
27+
28+
// problem: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
29+
// discuss: https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/discuss/?currentPage=1&orderBy=most_votes&query=
30+
31+
// submission codes start here
32+
33+
// Definition for singly-linked list.
34+
// #[derive(PartialEq, Eq, Clone, Debug)]
35+
// pub struct ListNode {
36+
// pub val: i32,
37+
// pub next: Option<Box<ListNode>>
38+
// }
39+
//
40+
// impl ListNode {
41+
// #[inline]
42+
// fn new(val: i32) -> Self {
43+
// ListNode {
44+
// next: None,
45+
// val
46+
// }
47+
// }
48+
// }
49+
// Definition for a binary tree node.
50+
// #[derive(Debug, PartialEq, Eq)]
51+
// pub struct TreeNode {
52+
// pub val: i32,
53+
// pub left: Option<Rc<RefCell<TreeNode>>>,
54+
// pub right: Option<Rc<RefCell<TreeNode>>>,
55+
// }
56+
//
57+
// impl TreeNode {
58+
// #[inline]
59+
// pub fn new(val: i32) -> Self {
60+
// TreeNode {
61+
// val,
62+
// left: None,
63+
// right: None
64+
// }
65+
// }
66+
// }
67+
use std::rc::Rc;
68+
use std::cell::RefCell;
69+
impl Solution {
70+
// pub fn sorted_list_to_bst(head: Option<Box<ListNode>>) -> Option<Rc<RefCell<TreeNode>>> {
71+
// if head.is_none() {
72+
// return None;
73+
// }
74+
// Self::dfs(&head, 0, (nums.len() - 1) as i32)
75+
// }
76+
//
77+
// fn dfs(nums: &Option<Box<ListNode>>, left: i32, right: i32) -> Option<Rc<RefCell<TreeNode>>> {
78+
//
79+
// }
80+
}
81+
82+
// submission codes end
83+
84+
#[cfg(test)]
85+
mod tests {
86+
use super::*;
87+
88+
#[test]
89+
fn test_109() {
90+
}
91+
}

0 commit comments

Comments
 (0)