Leetcode 653. Two Sum IV
来源:互联网 发布:知乎45个神回复 编辑:程序博客网 时间:2024/05/20 14:42
恩,还是给一个数,问有给定的二叉搜索树,有没有两个数相加的和等于这个数
恩,我是最直接的做法,开空间记录出现过的数字。。
其实,这道题的思想,我猜应该是要求一个空间为O(1)的解法。。但是觉得这么做太复杂,就算了吧
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.Example 1:Input: 5 / \ 3 6 / \ \2 4 7Target = 9Output: TrueExample 2:Input: 5 / \ 3 6 / \ \2 4 7Target = 28Output: False
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def findTarget(self, root, k): """ :type root: TreeNode :type k: int :rtype: bool """ exists = set() # DFS def helper(root): if root is None: return False if k - root.val in exists: return True else: exists.add(root.val) return helper(root.left) or helper(root.right) return helper(root)
阅读全文
0 0
- leetcode 653. Two Sum IV
- Leetcode 653. Two Sum IV
- 【LeetCode】653. Two Sum IV
- Leetcode 653. Two Sum IV
- 【LeetCode】653. Two Sum IV
- leetcode 653. Two Sum IV
- leetcode 653. Two Sum IV
- [LeetCode]653. Two Sum IV
- leetcode: 653. Two Sum IV
- leetcode 653. Two Sum IV
- LeetCode 653. Two Sum IV
- LeetCode-653. Two Sum IV
- leetcode 653. Two Sum IV
- leetcode 653. Two Sum IV
- leetcode[Two Sum IV
- LeetCode Two Sum IV
- leetcode Two Sum IV
- [Leetcode] Binary tree--653. Two Sum IV
- Android Binder 修炼之道(三)Binder 系统C++实现 BpXXX代理类分析
- Ubuntu获取系统时间
- 配置网络yum源遇到的问题之解决办法
- JAVA排序算法 -- Arrays.sort()
- 服务器间并发下载技巧
- Leetcode 653. Two Sum IV
- UDP服务器
- ZIGBEE AD 转换 片内温度学习总结
- could not open 'D:\Java\lib\amd64\jvm.cfg
- 常量
- For-each循环优于for循环感悟
- [随心译]2017.8.10-随心译以后转到简书
- ZIGBEE 电源控制之中断唤醒
- Ubuntu获取GPS数据& 保存TXT