Skip to content

Commit f423928

Browse files
committed
[feat ]: leetcode 129
Signed-off-by: Bo-Wei Chen(BWbwchen) <tim.chenbw@gmail.com>
1 parent 260fcc3 commit f423928

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
pub fn sum_numbers(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
23+
Self::solve(root, 0)
24+
}
25+
fn solve(root: Option<Rc<RefCell<TreeNode>>>, base: i32) -> i32 {
26+
let mut ret = 0;
27+
28+
if let Some(root) = root {
29+
// check whether it is leaf node
30+
let mut node = root.borrow_mut();
31+
32+
if node.left.is_none() && node.right.is_none() {
33+
ret = 10 * base + node.val;
34+
} else {
35+
let left = Self::solve(node.left.take(), 10 * base + node.val);
36+
let right = Self::solve(node.right.take(), 10 * base + node.val);
37+
ret = left + right;
38+
}
39+
}
40+
41+
ret
42+
}
43+
}

0 commit comments

Comments
 (0)