Skip to content

Commit 71a0fdc

Browse files
committed
add 979
1 parent cf93296 commit 71a0fdc

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

S0979-distribute-coins-in-binary-tree/Cargo.lock

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "S0979-distribute-coins-in-binary-tree"
3+
version = "0.1.0"
4+
authors = ["xargin <cao1988228@163.com>"]
5+
edition = "2018"
6+
7+
[dependencies]
8+
leetcode_prelude="^0.1"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
struct Solution;
2+
use leetcode_prelude::*;
3+
4+
// Definition for a binary tree node.
5+
6+
use std::cell::RefCell;
7+
use std::rc::Rc;
8+
impl Solution {
9+
pub fn distribute_coins(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
10+
let mut res = 0;
11+
Solution::dfs(root, &mut res);
12+
res
13+
}
14+
15+
pub fn dfs(root: Option<Rc<RefCell<TreeNode>>>, res:&mut i32) -> i32 {
16+
if root.is_none() {
17+
return 0;
18+
}
19+
20+
let r_u_b = root.as_ref().unwrap().borrow_mut();
21+
let left_step_cnt = Solution::dfs(r_u_b.left.clone(), res);
22+
let right_step_cnt = Solution::dfs(r_u_b.right.clone(), res);
23+
//println!("{}, {}",left_step_cnt, right_step_cnt);
24+
*res += left_step_cnt.abs() + right_step_cnt.abs();
25+
26+
r_u_b.val - 1 + left_step_cnt + right_step_cnt
27+
}
28+
}
29+
30+
fn main() {
31+
let r = btree![3,0,0];
32+
//println!("{}", Solution::distribute_coins(r));
33+
println!("{}", Solution::distribute_coins(btree![1,0,0,null,3]));
34+
}

0 commit comments

Comments
 (0)