parameters: (self, root, distance)
return_statement: return iter_dfs(distance, root)
docstring: :type root: TreeNode
          :type distance: int
          :rtype: int
docstring_summary: :type root: TreeNode
          :type distance: int
          :rtype: int
docstring_tokens: : type root : TreeNode : type distance : int : rtype : int
function: 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)
function_tokens: def countPairs ( self , root , distance ) : 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 )