Number Of Good Leaf Node Pairs In Binary Tree
Count good leaf node pairs in binary tree: DFS traversal from each leaf node, stop when steps > distance. If another leaf within distance, add 1 to answer. Divide by 2 for double-counted pairs.
1530. Number of Good Leaf Nodes Pairs Medium 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:...
