forked from xhofe/daily-problem-of-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30-sum-of-root-to-leaf-binary-numbers.rs
43 lines (42 loc) · 1.16 KB
/
30-sum-of-root-to-leaf-binary-numbers.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
// https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/
// 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::rc::Rc;
use std::cell::RefCell;
impl Solution {
pub fn sum_root_to_leaf(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
fn dfs(node: Rc<RefCell<TreeNode>>, mut val: i32) -> i32 {
val <<= 1;
let node = node.borrow();
val |= val + node.val;
if node.left.is_none() && node.right.is_none() {
return val;
}
let mut ans = 0;
if node.left.is_some() {
ans += dfs(node.left.as_ref().unwrap().clone(), val);
}
if node.right.is_some() {
ans += dfs(node.right.as_ref().unwrap().clone(), val);
}
ans
}
dfs(root.unwrap(), 0)
}
}