Python straight forward DFS solution

  • 0
    def findTarget(self, root, k):
            nodes = set()
            def dfs(root):
                if not root: return False
                if root.val in nodes: return True
                nodes.add(k - root.val)
                return dfs(root.left) or dfs(root.right)
            return dfs(root)

Log in to reply

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