1530. Number of Good Leaf Nodes Pairs Medium
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 */
33pub struct Solution {}
34use crate::util::tree::{TreeNode, to_tree};
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// }
59use std::rc::Rc;
60use std::cell::RefCell;
61impl Solution {
62 pub fn count_pairs(root: Option<Rc<RefCell<TreeNode>>>, distance: i32) -> i32 {
63 0
64 }
65}
66
67// submission codes end
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72
73 #[test]
74 fn test_1530() {
75 }
76}
77
Back
© 2025 bowen.ge All Rights Reserved.