Skip to content

Commit e95f558

Browse files
update tree
1 parent d0d55d3 commit e95f558

File tree

4 files changed

+296
-0
lines changed

4 files changed

+296
-0
lines changed

src/solution/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ mod s1011_capacity_to_ship_packages_within_d_days;
1414
mod s0137_single_number_ii;
1515
mod s0403_frog_jump;
1616
mod s0633_sum_of_square_numbers;
17+
mod s0112_path_sum;
18+
mod s0129_sum_root_to_leaf_numbers;
19+
mod s0814_binary_tree_pruning;

src/solution/s0112_path_sum.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* [112] Path Sum
3+
*
4+
* Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.
5+
* A leaf is a node with no children.
6+
*
7+
* Example 1:
8+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum1.jpg" style="width: 500px; height: 356px;" />
9+
* Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
10+
* Output: true
11+
*
12+
* Example 2:
13+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/01/18/pathsum2.jpg" />
14+
* Input: root = [1,2,3], targetSum = 5
15+
* Output: false
16+
*
17+
* Example 3:
18+
*
19+
* Input: root = [1,2], targetSum = 0
20+
* Output: false
21+
*
22+
*
23+
* Constraints:
24+
*
25+
* The number of nodes in the tree is in the range [0, 5000].
26+
* -1000 <= Node.val <= 1000
27+
* -1000 <= targetSum <= 1000
28+
*
29+
*/
30+
pub struct Solution {}
31+
use crate::util::tree::{TreeNode, to_tree};
32+
33+
// problem: https://leetcode.com/problems/path-sum/
34+
// discuss: https://leetcode.com/problems/path-sum/discuss/?currentPage=1&orderBy=most_votes&query=
35+
36+
// submission codes start here
37+
38+
// Definition for a binary tree node.
39+
// #[derive(Debug, PartialEq, Eq)]
40+
// pub struct TreeNode {
41+
// pub val: i32,
42+
// pub left: Option<Rc<RefCell<TreeNode>>>,
43+
// pub right: Option<Rc<RefCell<TreeNode>>>,
44+
// }
45+
//
46+
// impl TreeNode {
47+
// #[inline]
48+
// pub fn new(val: i32) -> Self {
49+
// TreeNode {
50+
// val,
51+
// left: None,
52+
// right: None
53+
// }
54+
// }
55+
// }
56+
use std::rc::Rc;
57+
use std::cell::RefCell;
58+
impl Solution {
59+
pub fn has_path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> bool {
60+
if let Some(p) = root {
61+
let left = p.borrow_mut().left.take();
62+
let right = p.borrow_mut().right.take();
63+
64+
let val = p.borrow_mut().val;
65+
66+
if left.is_none() && right.is_none() && val == target_sum {
67+
return true;
68+
}
69+
if Self::has_path_sum(left, target_sum - val) {
70+
return true;
71+
}
72+
if Self::has_path_sum(right, target_sum - val) {
73+
return true;
74+
}
75+
}
76+
false
77+
}
78+
}
79+
80+
// submission codes end
81+
82+
#[cfg(test)]
83+
mod tests {
84+
use super::*;
85+
86+
#[test]
87+
fn test_112() {
88+
assert_eq!(Solution::has_path_sum(tree![5,4,8,11,null,13,4,7,2,null,null,null,1], 22), true);
89+
assert_eq!(Solution::has_path_sum(tree![1, 2, 3], 5), false);
90+
assert_eq!(Solution::has_path_sum(tree![1, 2], 0), false);
91+
}
92+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* [129] Sum Root to Leaf Numbers
3+
*
4+
* You are given the root of a binary tree containing digits from 0 to 9 only.
5+
* Each root-to-leaf path in the tree represents a number.
6+
*
7+
* For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123.
8+
*
9+
* Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer.
10+
* A leaf node is a node with no children.
11+
*
12+
* Example 1:
13+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/num1tree.jpg" style="width: 212px; height: 182px;" />
14+
* Input: root = [1,2,3]
15+
* Output: 25
16+
* Explanation:
17+
* The root-to-leaf path 1->2 represents the number 12.
18+
* The root-to-leaf path 1->3 represents the number 13.
19+
* Therefore, sum = 12 + 13 = 25.
20+
*
21+
* Example 2:
22+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/02/19/num2tree.jpg" style="width: 292px; height: 302px;" />
23+
* Input: root = [4,9,0,5,1]
24+
* Output: 1026
25+
* Explanation:
26+
* The root-to-leaf path 4->9->5 represents the number 495.
27+
* The root-to-leaf path 4->9->1 represents the number 491.
28+
* The root-to-leaf path 4->0 represents the number 40.
29+
* Therefore, sum = 495 + 491 + 40 = 1026.
30+
*
31+
*
32+
* Constraints:
33+
*
34+
* The number of nodes in the tree is in the range [1, 1000].
35+
* 0 <= Node.val <= 9
36+
* The depth of the tree will not exceed 10.
37+
*
38+
*/
39+
pub struct Solution {}
40+
use crate::util::tree::{TreeNode, to_tree};
41+
42+
// problem: https://leetcode.com/problems/sum-root-to-leaf-numbers/
43+
// discuss: https://leetcode.com/problems/sum-root-to-leaf-numbers/discuss/?currentPage=1&orderBy=most_votes&query=
44+
45+
// submission codes start here
46+
47+
// Definition for a binary tree node.
48+
// #[derive(Debug, PartialEq, Eq)]
49+
// pub struct TreeNode {
50+
// pub val: i32,
51+
// pub left: Option<Rc<RefCell<TreeNode>>>,
52+
// pub right: Option<Rc<RefCell<TreeNode>>>,
53+
// }
54+
//
55+
// impl TreeNode {
56+
// #[inline]
57+
// pub fn new(val: i32) -> Self {
58+
// TreeNode {
59+
// val,
60+
// left: None,
61+
// right: None
62+
// }
63+
// }
64+
// }
65+
use std::rc::Rc;
66+
use std::cell::RefCell;
67+
impl Solution {
68+
pub fn sum_numbers(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
69+
let mut res = 0;
70+
Self::helper(root, &mut res, 0);
71+
return res;
72+
}
73+
74+
pub fn helper(root: Option<Rc<RefCell<TreeNode>>>, res: &mut i32, path: i32) {
75+
if let Some(p) = root {
76+
let left = p.borrow_mut().left.take();
77+
let right = p.borrow_mut().right.take();
78+
let val = p.borrow_mut().val;
79+
if left.is_none() && right.is_none() {
80+
*res += path * 10 + val;
81+
}
82+
if left.is_some() {
83+
Self::helper(left, res, path * 10 + val);
84+
}
85+
if right.is_some() {
86+
Self::helper(right, res, path * 10 + val);
87+
}
88+
}
89+
}
90+
}
91+
92+
// submission codes end
93+
94+
#[cfg(test)]
95+
mod tests {
96+
use super::*;
97+
98+
#[test]
99+
fn test_129() {
100+
assert_eq!(Solution::sum_numbers(tree![1, 2, 3]), 25);
101+
assert_eq!(Solution::sum_numbers(tree![4,9,0,5,1]), 1026);
102+
}
103+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* [814] Binary Tree Pruning
3+
*
4+
* We are given the head node root of a binary tree, where additionally every node's value is either a 0 or a 1.
5+
* Return the same tree where every subtree (of the given tree) not containing a 1 has been removed.
6+
* (Recall that the subtree of a node X is X, plus every node that is a descendant of X.)
7+
*
8+
* Example 1:
9+
* Input: [1,null,0,0,1]
10+
* Output: [1,null,0,null,1]
11+
*
12+
* Explanation:
13+
* Only the red nodes satisfy the property "every subtree not containing a 1".
14+
* The diagram on the right represents the answer.
15+
* <img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_2.png" style="width:450px" />
16+
*
17+
* Example 2:
18+
* Input: [1,0,1,0,0,0,1]
19+
* Output: [1,null,1,null,1]
20+
*
21+
* <img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/06/1028_1.png" style="width:450px" />
22+
*
23+
* Example 3:
24+
* Input: [1,1,0,1,1,0,1,0]
25+
* Output: [1,1,0,1,1,null,1]
26+
*
27+
* <img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/04/05/1028.png" style="width:450px" />
28+
*
29+
* Note:
30+
*
31+
* The binary tree will have at most 200 nodes.
32+
* The value of each node will only be 0 or 1.
33+
*
34+
*/
35+
pub struct Solution {}
36+
use crate::util::tree::{TreeNode, to_tree};
37+
38+
// problem: https://leetcode.com/problems/binary-tree-pruning/
39+
// discuss: https://leetcode.com/problems/binary-tree-pruning/discuss/?currentPage=1&orderBy=most_votes&query=
40+
41+
// submission codes start here
42+
43+
// Definition for a binary tree node.
44+
// #[derive(Debug, PartialEq, Eq)]
45+
// pub struct TreeNode {
46+
// pub val: i32,
47+
// pub left: Option<Rc<RefCell<TreeNode>>>,
48+
// pub right: Option<Rc<RefCell<TreeNode>>>,
49+
// }
50+
//
51+
// impl TreeNode {
52+
// #[inline]
53+
// pub fn new(val: i32) -> Self {
54+
// TreeNode {
55+
// val,
56+
// left: None,
57+
// right: None
58+
// }
59+
// }
60+
// }
61+
use std::rc::Rc;
62+
use std::cell::RefCell;
63+
impl Solution {
64+
pub fn prune_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
65+
if let Some(p) = root {
66+
let left = p.borrow_mut().left.take();
67+
let left_a = Self::prune_tree(left);
68+
69+
let right = p.borrow_mut().right.take();
70+
let right_a = Self::prune_tree(right);
71+
72+
if left_a.is_none() && right_a.is_none() && p.borrow_mut().val == 0 {
73+
return None;
74+
} else {
75+
p.borrow_mut().left = left_a;
76+
p.borrow_mut().right = right_a;
77+
}
78+
return Some(p);
79+
}
80+
None
81+
}
82+
}
83+
84+
// submission codes end
85+
86+
#[cfg(test)]
87+
mod tests {
88+
use super::*;
89+
90+
#[test]
91+
fn test_814() {
92+
assert_eq!(Solution::prune_tree(tree![1,0,1,0,0,0,1]), tree![1,null,1,null,1]);
93+
assert_eq!(Solution::prune_tree(tree![1,null,0,0,1]), tree![1,null,0,null,1]);
94+
assert_eq!(Solution::prune_tree(tree![1,1,0,1,1,0,1,0]), tree![1,1,0,1,1,null,1]);
95+
assert_eq!(Solution::prune_tree(tree![0, 0, 0]), tree![]);
96+
assert_eq!(Solution::prune_tree(tree![0]), tree![]);
97+
}
98+
}

0 commit comments

Comments
 (0)