34 is inscribed: binary tree and a path value

# - * - Coding: UTF-. 8 - * - 
# the TreeNode class: 
# DEF the __init __ (Self, X): 
# = X self.val 
# self.left = None 
# self.right = None 
Import Copy 
class Solution: 
    # Returns two-dimensional list, found inside each list represents the path 
    DEF FindPath (Self, root, expectNumber): 
        # the Write code here Wallpaper 
        # first determine the boundaries 
        IF root == None: 
            return [] 
        
        # breadth-first traversal of our tree, breadth-first has a secondary configuration list 
        Support = [the root] 
        supportArray = [[root.val]] 
        retArray = [] 
        
        the while Support: 
            TEMP = Support [0] 
            tempsupportArray supportArray = [0] 
            # is necessary to determine whether the current node is a leaf node
            if temp.left==None and temp.right==None:
                tempSum = sum(tempsupportArray)
                if tempSum==expectNumber:
                    retArray.insert(0,tempsupportArray)
            if temp.left:
                support.append(temp.left)
                #这里为啥用copy????
                newtempsupportArray = copy.copy(tempsupportArray)
                newtempsupportArray.append(temp.left.val)
                supportArray.append(newtempsupportArray)
            if temp.right:
                support.append(temp.right)
                newtempsupportArray = copy.copy(tempsupportArray)
                newtempsupportArray.append(temp.right.val)
                supportArray.append(newtempsupportArray)
                
            del supportArray[0]
            del support[0]
        return retArray 

  

Guess you like

Origin www.cnblogs.com/ivyharding/p/11357176.html