Python DFS


  • 0
    class Solution(object):
        def sumOfLeftLeaves(self, root):
            self.ans = 0
            def dfs(r, d):
                if not r:
                    return
                if not r.left and not r.right and d:
                    self.ans += r.val
                dfs(r.left, 1)
                dfs(r.right, 0)
            dfs(root, 0)
            return self.ans
    

Log in to reply
 

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