Problem solution · Python

Number of Good Leaf Nodes Pairs

Number of Good Leaf Nodes Pairs: a Python solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Depth-first search
Source
Kamyu LeetCode Solutions
Length
76 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Depth-first search

For Number of Good Leaf Nodes Pairs, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 76 lines of Python from the credited upstream file number-of-good-leaf-nodes-pairs.py.
  • The implementation visibly relies on hash lookup.
  • No explicit loop blocks detected, together with recursive traversal.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeNumber of Good Leaf Nodes Pairs · PythonPython
Use this to learn the idea, then write your own version.
# Time:  O(n)# Space: O(h) import collections  # Definition for a binary tree node.class TreeNode(object):    def __init__(self, val=0, left=None, right=None):        self.val = val        self.left = left        self.right = right  class Solution(object):    def countPairs(self, root, distance):        """        :type root: TreeNode        :type distance: int        :rtype: int        """        def iter_dfs(distance, root):            result = 0            stk = [(1, (root, [collections.Counter()]))]            while stk:                step, params = stk.pop()                if step == 1:                    node, ret = params                    if not node:                        continue                    if not node.left and not node.right:                        ret[0][0] = 1                        continue                    left, right = [collections.Counter()], [collections.Counter()]                    stk.append((2, (left, right, ret)))                    stk.append((1, (node.right, right)))                    stk.append((1, (node.left, left)))                else:                    left, right, ret = params                    for left_d, left_c in left[0].iteritems():                        for right_d,right_c in right[0].iteritems():                            if left_d+right_d+2 <= distance:                                result += left_c*right_c                    ret[0] = collections.Counter({k+1:v for k,v in (left[0]+right[0]).iteritems()})            return result                return iter_dfs(distance, root)  # Time:  O(n)# Space: O(h)import collections  class Solution2(object):    def countPairs(self, root, distance):        """        :type root: TreeNode        :type distance: int        :rtype: int        """        def dfs(distance, node):            if not node:                return 0, collections.Counter()            if not node.left and not node.right:                return 0, collections.Counter([0])            left, right = dfs(distance, node.left), dfs(distance, node.right)            result = left[0]+right[0]            for left_d, left_c in left[1].iteritems():                for right_d,right_c in right[1].iteritems():                    if left_d+right_d+2 <= distance:                        result += left_c*right_c            return result, collections.Counter({k+1:v for k,v in (left[1]+right[1]).iteritems()})                return dfs(distance, root)[0] 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗