1653 Number Of Good Leaf Nodes Pairs
1653 Number Of Good Leaf Nodes Pairs
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:
1
2
3
4
5
**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:
1
2
3
4
5
**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:
1
2
3
4
5
**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:
1
2
3
The number of nodes in the tree is in the range [1, 210].
1 <= Node.val <= 100
1 <= distance <= 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution:
def _traverse_tree(self, curr_node, prev_node, graph, leaf_nodes):
if curr_node is None:
return
if curr_node.left is None and curr_node.right is None:
leaf_nodes.add(curr_node)
if prev_node is not None:
if prev_node not in graph:
graph[prev_node] = []
graph[prev_node].append(curr_node)
if curr_node not in graph:
graph[curr_node] = []
graph[curr_node].append(prev_node)
self._traverse_tree(curr_node.left, curr_node, graph, leaf_nodes)
self._traverse_tree(curr_node.right, curr_node, graph, leaf_nodes)
def countPairs(self, root, distance):
graph = {}
leaf_nodes = set()
self._traverse_tree(root, None, graph, leaf_nodes)
ans = 0
for leaf in leaf_nodes:
bfs_queue = []
seen = set()
bfs_queue.append(leaf)
seen.add(leaf)
for i in range(distance + 1):
# Clear all nodes in the queue (distance i away from leaf node)
# Add the nodes' neighbors (distance i+1 away from leaf node)
size = len(bfs_queue)
for j in range(size):
curr_node = bfs_queue.pop(0)
if curr_node in leaf_nodes and curr_node != leaf:
ans += 1
if curr_node in graph:
for neighbor in graph.get(curr_node):
if neighbor not in seen:
bfs_queue.append(neighbor)
seen.add(neighbor)
return ans // 2
This post is licensed under CC BY 4.0 by the author.

