-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_one_row_to_tree_623.rs
executable file
·62 lines (60 loc) · 1.55 KB
/
add_one_row_to_tree_623.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
62
use crate::Solution;
// 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 crate::TreeNode;
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
pub fn add_one_row(
root: Option<Rc<RefCell<TreeNode>>>,
v: i32,
d: i32,
) -> Option<Rc<RefCell<TreeNode>>> {
if d == 1 {
Some(Rc::new(RefCell::new(TreeNode {
val: v,
left: root,
right: None,
})))
} else {
Self::dfs(&root, v, d - 2);
root
}
}
fn dfs(node: &Option<Rc<RefCell<TreeNode>>>, v: i32, d: i32) {
if let Some(n) = node {
if d > 0 {
Self::dfs(&n.borrow().left, v, d - 1);
Self::dfs(&n.borrow().right, v, d - 1);
} else {
let mut n = n.borrow_mut();
n.left = Some(Rc::new(RefCell::new(TreeNode {
val: v,
left: n.left.take(),
right: None,
})));
n.right = Some(Rc::new(RefCell::new(TreeNode {
val: v,
left: None,
right: n.right.take(),
})));
}
}
}
}