Skip to content

Commit ea00e7d

Browse files
committed
1530. Number of Good Leaf Nodes Pairs: AC
1 parent 506f7d8 commit ea00e7d

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1154,3 +1154,4 @@ mod s1525_number_of_good_ways_to_split_a_string;
11541154
mod s1526_minimum_number_of_increments_on_subarrays_to_form_a_target_array;
11551155
mod s1528_shuffle_string;
11561156
mod s1529_minimum_suffix_flips;
1157+
mod s1530_number_of_good_leaf_nodes_pairs;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
* [1530] Number of Good Leaf Nodes Pairs
3+
*
4+
* You are given the root of a binary tree and an integer distance. A pair of two different leaf nodes of a binary tree is said to be good if the length of the shortest path between them is less than or equal to distance.
5+
* Return the number of good leaf node pairs in the tree.
6+
*
7+
* Example 1:
8+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/07/09/e1.jpg" style="width: 250px; height: 250px;" />
9+
* Input: root = [1,2,3,null,4], distance = 3
10+
* Output: 1
11+
* Explanation: The leaf nodes of the tree are 3 and 4 and the length of the shortest path between them is 3. This is the only good pair.
12+
*
13+
* Example 2:
14+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/07/09/e2.jpg" style="width: 250px; height: 182px;" />
15+
* Input: root = [1,2,3,4,5,6,7], distance = 3
16+
* Output: 2
17+
* Explanation: The good pairs are [4,5] and [6,7] with shortest path = 2. The pair [4,6] is not good because the length of ther shortest path between them is 4.
18+
*
19+
* Example 3:
20+
*
21+
* Input: root = [7,1,4,6,null,5,3,null,null,null,null,null,2], distance = 3
22+
* Output: 1
23+
* Explanation: The only good pair is [2,5].
24+
*
25+
*
26+
* Constraints:
27+
*
28+
* The number of nodes in the tree is in the range [1, 2^10].
29+
* 1 <= Node.val <= 100
30+
* 1 <= distance <= 10
31+
*
32+
*/
33+
pub struct Solution {}
34+
use crate::util::tree::{to_tree, TreeNode};
35+
36+
// problem: https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/
37+
// discuss: https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/discuss/?currentPage=1&orderBy=most_votes&query=
38+
39+
// submission codes start here
40+
41+
// Definition for a binary tree node.
42+
// #[derive(Debug, PartialEq, Eq)]
43+
// pub struct TreeNode {
44+
// pub val: i32,
45+
// pub left: Option<Rc<RefCell<TreeNode>>>,
46+
// pub right: Option<Rc<RefCell<TreeNode>>>,
47+
// }
48+
//
49+
// impl TreeNode {
50+
// #[inline]
51+
// pub fn new(val: i32) -> Self {
52+
// TreeNode {
53+
// val,
54+
// left: None,
55+
// right: None
56+
// }
57+
// }
58+
// }
59+
use std::cell::RefCell;
60+
use std::rc::Rc;
61+
impl Solution {
62+
pub fn count_pairs(root: Option<Rc<RefCell<TreeNode>>>, distance: i32) -> i32 {
63+
let mut result = 0;
64+
Self::dfs_helper(&root, distance, &mut result);
65+
result
66+
}
67+
68+
fn dfs_helper(root: &Option<Rc<RefCell<TreeNode>>>, distance: i32, ans: &mut i32) -> Vec<i32> {
69+
match root {
70+
None => vec![0; distance as usize + 1],
71+
Some(node) => {
72+
let node = node.borrow();
73+
if node.left.is_none() && node.right.is_none() {
74+
let mut v = vec![0; distance as usize + 1];
75+
v[1] += 1;
76+
return v;
77+
}
78+
let left = Self::dfs_helper(&node.left, distance, ans);
79+
let right = Self::dfs_helper(&node.right, distance, ans);
80+
for i in 0..distance {
81+
for j in (0..distance).rev() {
82+
if i as i32 + j <= distance {
83+
*ans += left[i as usize] * right[j as usize];
84+
}
85+
}
86+
}
87+
let mut v = vec![0; distance as usize + 1];
88+
89+
for i in (1..v.len() - 1).rev() {
90+
v[i + 1] = left[i] + right[i];
91+
}
92+
v
93+
}
94+
}
95+
}
96+
}
97+
98+
// submission codes end
99+
100+
#[cfg(test)]
101+
mod tests {
102+
use super::*;
103+
104+
#[test]
105+
fn test_1530_example_1() {
106+
let root = tree![1, 2, 3, null, 4];
107+
let distance = 3;
108+
109+
let result = 1;
110+
111+
assert_eq!(Solution::count_pairs(root, distance), result);
112+
}
113+
114+
#[test]
115+
fn test_1530_example_2() {
116+
let root = tree![1, 2, 3, 4, 5, 6, 7];
117+
let distance = 3;
118+
119+
let result = 2;
120+
121+
assert_eq!(Solution::count_pairs(root, distance), result);
122+
}
123+
124+
#[test]
125+
fn test_1530_example_3() {
126+
let root = tree![7, 1, 4, 6, null, 5, 3, null, null, null, null, null, 2];
127+
let distance = 3;
128+
129+
let result = 1;
130+
131+
assert_eq!(Solution::count_pairs(root, distance), result);
132+
}
133+
}

0 commit comments

Comments
 (0)