forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
61 lines (59 loc) · 1.45 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
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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::rc::Rc;
impl Solution {
pub fn get_all_elements(
root1: Option<Rc<RefCell<TreeNode>>>,
root2: Option<Rc<RefCell<TreeNode>>>
) -> Vec<i32> {
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, t: &mut Vec<i32>) {
if let Some(root) = root {
dfs(&root.borrow().left, t);
t.push(root.borrow().val);
dfs(&root.borrow().right, t);
}
}
let mut t1 = Vec::new();
let mut t2 = Vec::new();
dfs(&root1, &mut t1);
dfs(&root2, &mut t2);
let mut ans = Vec::new();
let mut i = 0;
let mut j = 0;
while i < t1.len() && j < t2.len() {
if t1[i] < t2[j] {
ans.push(t1[i]);
i += 1;
} else {
ans.push(t2[j]);
j += 1;
}
}
while i < t1.len() {
ans.push(t1[i]);
i += 1;
}
while j < t2.len() {
ans.push(t2[j]);
j += 1;
}
ans
}
}