Share BFS Python, Easy to Understand


  • 0
    Y
    class Solution(object):
        def findTarget(self, root, k):
            if not root or (root.left is None and root.right is None): return False
            import collections
            ls=[]
            queue=collections.deque([root])
            while queue:
                node=queue.popleft()
                ls.append(node.val)
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            for i in ls:
                if (k-i) in ls and k!=2*i:
                    return True
            return False
    

Log in to reply
 

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