# LeetCode 1530. Number of Good Leaf Nodes Pairs

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.

Return the number of good leaf node pairs in the tree.

Example 1:

Input: root = [1,2,3,null,4], distance = 3
Output: 1
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.


Example 2:

Input: root = [1,2,3,4,5,6,7], distance = 3
Output: 2
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.


Example 3:

Input: root = [7,1,4,6,null,5,3,null,null,null,null,null,2], distance = 3
Output: 1
Explanation: The only good pair is [2,5].

Constraints:

• The number of nodes in the tree is in the range [1, 210].
• 1 <= Node.val <= 100
• 1 <= distance <= 10

The good pair is distance between leaves is <= threshold.

For the current root, we need to know for left and right subtree, the leaves distance and its corresponding count. distance : count.

Iterate both left and right, if distance sum <= threshold. Accumlate the product to the result.

Since returned array is used by one level up, thus when root is leaf, set resArr[1] as one, but not resArr[0] as one.

Time Complexity: O(n * distance ^ 2).

Space: O(n*distance). For the balanced tree, the leaf level has n / 2 nodes. Each node has distance array.

AC Java:

 1 /**
2  * Definition for a binary tree node.
3  * public class TreeNode {
4  *     int val;
5  *     TreeNode left;
6  *     TreeNode right;
7  *     TreeNode() {}
8  *     TreeNode(int val) { this.val = val; }
9  *     TreeNode(int val, TreeNode left, TreeNode right) {
10  *         this.val = val;
11  *         this.left = left;
12  *         this.right = right;
13  *     }
14  * }
15  */
16 class Solution {
17     int result = 0;
18     public int countPairs(TreeNode root, int distance) {
19         if(distance <= 1){
20             return result;
21         }
22
23         dfs(root, distance);
24         return result;
25     }
26
27     private int[] dfs(TreeNode root, int distance){
28         int[] resArr = new int[distance];
29         if(root == null){
30             return resArr;
31         }
32
33         if(root.left == null && root.right == null){
34             resArr[1] = 1;
35             return resArr;
36         }
37
38         int[] left = dfs(root.left, distance);
39         int[] right = dfs(root.right, distance);
40         for(int l = 1; l < left.length; l++){
41             for(int r = 1; r < right.length; r++){
42                 if(l + r <= distance){
43                     result += left[l] * right[r];
44                 }
45             }
46         }
47
48         for(int i = 1; i < resArr.length; i++){
49             resArr[i] = left[i - 1] + right[i - 1];
50         }
51
52         return resArr;
53     }
54 }

posted @ 2024-07-15 11:37  Dylan_Java_NYC  阅读(26)  评论(0编辑  收藏  举报