[Leetcode] Binary tree--653. Two Sum IV
2017-08-27 11:17
369 查看
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:
Example 2:
Solution:
Example 1:
Input: 5 / \ 3 6 / \ \ 2 4 7 Target = 9 Output: True
Example 2:
Input: 5 / \ 3 6 / \ \ 2 4 7 Target = 28 Output: False
Solution:
#traverse the tree and using's hash to store visited node dic = {} def helperRecursive(root): if root is None: return False if (k - root.val) in dic: return True if root.val not in dic: dic[root.val] = 1 return helperRecursive(root.left) or helperRecursive(root.right) return helperRecursive(root)
相关文章推荐
- [Leetcode] Binary tree--653. Two Sum IV
- leetcode 每日一题 104. Maximum Depth of Binary Tree
- [leetcode] 111. Minimum Depth of Binary Tree
- leetcode 653. Two Sum IV - Input is a BST
- [Leetcode] Binary tree--112. Path Sum
- LeetCode 543. Diameter of Binary Tree
- [leetcode] 543. Diameter of Binary Tree
- LeetCode543. Diameter of Binary Tree
- 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
- [勇者闯LeetCode] 104. Maximum Depth of Binary Tree
- LeetCode-653. Two Sum IV - Input is a BST
- [LeetCode]104. Maximum Depth of Binary Tree
- Leetcode 111. Minimum Depth of Binary Tree
- LeetCode-104. Maximum Depth of Binary Tree
- leetcode 653. Two Sum IV - Input is a BST两个二叉搜索树中的和
- [leetcode: Python] 111. Minimum Depth of Binary Tree
- [leetcode]-543. Diameter of Binary Tree
- leetcode 543. Diameter of Binary Tree
- Leetcode 543. Diameter of Binary Tree
- 【LeetCode】653. Two Sum IV - Input is a BST 解题报告