Branch Sums

Tiffany ·2024년 3월 8일
0

AlgoExpert

목록 보기
9/20

class BinaryTree:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

def getSum(root, result, curr): 
    if not root.left and not root.right:
        result.append(curr) 
    #curr += root.val 
    if root.left:
        getSum(root.left, result, curr+root.left.value) 
    if root.right:
        getSum(root.right, result, curr+root.right.value) 
    return result 
def branchSums(root):
    if not root:
        return [] 
    return getSum(root, [], root.value) 
    
profile
Love what you do and don't quit.

0개의 댓글