美文网首页
653. Two Sum IV - Input is a BST

653. Two Sum IV - Input is a BST

作者: 一个想当大佬的菜鸡 | 来源:发表于2019-05-31 18:20 被阅读0次
    653. Two Sum IV - Input is a BST

    很简单的题,可以深度优先,也可广度优先。

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def findTarget(self, root, k):
            """
            :type root: TreeNode
            :type k: int
            :rtype: bool
            """
            myset = set()
            qu = [root]
            while qu:
                node = qu.pop(0)
                if k - node.val in myset:
                    return True
                else:
                    myset.add(node.val)
                if node.left:
                    qu.append(node.left)
                if node.right:
                    qu.append(node.right)
            return False
    

    相关文章

      网友评论

          本文标题:653. Two Sum IV - Input is a BST

          本文链接:https://www.haomeiwen.com/subject/hbwgtctx.html