forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
48 lines (47 loc) · 1.24 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
48
// 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(preorder: Vec<i32>, inorder: Vec<i32>) -> Option<Rc<RefCell<TreeNode>>> {
let mut d = HashMap::new();
for (i, &x) in inorder.iter().enumerate() {
d.insert(x, i);
}
Self::dfs(&preorder, &d, 0, 0, preorder.len())
}
pub fn dfs(
preorder: &Vec<i32>,
d: &HashMap<i32, usize>,
i: usize,
j: usize,
n: usize,
) -> Option<Rc<RefCell<TreeNode>>> {
if n <= 0 {
return None;
}
let v = preorder[i];
let k = d[&v];
let mut root = TreeNode::new(v);
root.left = Self::dfs(preorder, d, i + 1, j, k - j);
root.right = Self::dfs(preorder, d, i + k - j + 1, k + 1, n - k + j - 1);
Some(Rc::new(RefCell::new(root)))
}
}