Golang recursive DFS


  • 0
    R
    func pathSum(root *TreeNode, sum int) int {
        if root == nil { return 0 }
        return sumUp(root, 0, sum) + pathSum(root.Left, sum) + pathSum(root.Right, sum)
    }
    
    func sumUp(root *TreeNode, pre int, sum int) int {
        if root == nil { return 0 }
        current := pre + root.Val
        
        c:= 0
        
        if current == sum { c = 1 }
        return c + sumUp(root.Left, current, sum) + sumUp(root.Right, current, sum)
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.