-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.rs
47 lines (47 loc) · 1.3 KB
/
Solution.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
impl Solution {
pub fn build_tree(inorder: Vec<i32>, postorder: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
let n = inorder.len();
let mut d: HashMap<i32, usize> = HashMap::new();
for i in 0..n {
d.insert(inorder[i], i);
}
fn dfs(
postorder: &[i32],
d: &HashMap<i32, usize>,
i: usize,
j: usize,
n: usize,
) -> Option<Rc<RefCell<TreeNode>>> {
if n <= 0 {
return None;
}
let val = postorder[j + n - 1];
let k = *d.get(&val).unwrap();
let left = dfs(postorder, d, i, j, k - i);
let right = dfs(postorder, d, k + 1, j + k - i, n - 1 - (k - i));
Some(Rc::new(RefCell::new(TreeNode { val, left, right })))
}
dfs(&postorder, &d, 0, 0, n)
}
}