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 7

Target = 9

Output: True
Example 2:

Input:

    5
/ \
3 6
/ \ \
2 4 7

Target = 28

Output: False

思路:

Two Sum的变种题,这次输入的是一个二叉树,还是用HashMap,然后遍历二叉树,用之前的方法找就行了。

还有一种方法是利用BST的性质,进行查找。

Java:

This method also works for those who are not BSTs. The idea is to use a hashtable to save the values of the nodes in the BST. Each time when we insert the value of a new node into the hashtable, we check if the hashtable contains k - node.val.

Time Complexity: O(n), Space Complexity: O(n).

public boolean findTarget(TreeNode root, int k) {
HashSet<Integer> set = new HashSet<>();
return dfs(root, set, k);
} public boolean dfs(TreeNode root, HashSet<Integer> set, int k){
if(root == null)return false;
if(set.contains(k - root.val))return true;
set.add(root.val);
return dfs(root.left, set, k) || dfs(root.right, set, k);
}

Java:

The idea is to use a sorted array to save the values of the nodes in the BST by using an inorder traversal. Then, we use two pointers which begins from the start and end of the array to find if there is a sum k.

Time Complexity: O(n), Space Complexity: O(n).

    public boolean findTarget(TreeNode root, int k) {
List<Integer> nums = new ArrayList<>();
inorder(root, nums);
for(int i = 0, j = nums.size()-1; i<j;){
if(nums.get(i) + nums.get(j) == k)return true;
if(nums.get(i) + nums.get(j) < k)i++;
else j--;
}
return false;
} public void inorder(TreeNode root, List<Integer> nums){
if(root == null)return;
inorder(root.left, nums);
nums.add(root.val);
inorder(root.right, nums);
}

Java:

The idea is to use binary search method. For each node, we check if k - node.val exists in this BST.

Time Complexity: O(nh), Space Complexity: O(h). h is the height of the tree, which is logn at best case, and n at worst case.

    public boolean findTarget(TreeNode root, int k) {
return dfs(root, root, k);
} public boolean dfs(TreeNode root, TreeNode cur, int k){
if(cur == null)return false;
return search(root, cur, k - cur.val) || dfs(root, cur.left, k) || dfs(root, cur.right, k);
} public boolean search(TreeNode root, TreeNode cur, int value){
if(root == null)return false;
return (root.val == value) && (root != cur)
|| (root.val < value) && search(root.right, cur, value)
|| (root.val > value) && search(root.left, cur, value);
}

Java:

public class Solution {
public boolean findTarget(TreeNode root, int k) {
List<Integer> list = new ArrayList<>();
inorder(root, list);
int i = 0;
int j = list.size() - 1;
while (i < j) {
int sum = list.get(i) + list.get(j);
if (sum == k) {
return true;
}
else if (sum < k) {
i++;
}
else {
j--;
}
}
return false;
} public List<Integer> inorder(TreeNode root, List<Integer> list) {
Stack<TreeNode> stack = new Stack<>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
list.add(root.val);
root = root.right;
}
return list;
}
}

Java:

public class Solution {
public boolean findTarget(TreeNode root, int k) {
Set<Integer> candidates = new HashSet<>();
Stack<TreeNode> stack = new Stack<>();
while (!stack.empty() || root != null) {
if (root != null) {
int val = root.val;
if (candidates.contains(val)) {
return true;
} else {
candidates.add(k - val);
}
stack.add(root);
root = root.left;
} else {
TreeNode node = stack.pop();
root = node.right;
}
}
return false;
}
}  

Python: 递归遍历BST + Two Sum

class Solution(object):
def findTarget(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: bool
"""
self.dset = set()
self.traverse(root)
for n in self.dset:
if k - n != n and k - n in self.dset:
return True
return False
def traverse(self, root):
if not root: return
self.dset.add(root.val)
self.traverse(root.left)
self.traverse(root.right)

Python: wo, 160 ms, faster than 9.23% of Python online submissions

class Solution(object):
def findTarget(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: bool
"""
nums = []
self.dfs(root, nums)
lookup = [] for num in nums:
if k - num in lookup:
return True
lookup.append(num)
return False def dfs(self, root, res):
if not root:
return
res.append(root.val)
self.dfs(root.left, res)
self.dfs(root.right, res)

Python: 递归遍历BST + 利用BST性质进行检索

class Solution(object):
def findTarget(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: bool
"""
self.root = root
self.k = k
return self.findNumber(root)
def findNumber(self, root):
if not root: return False
node = self.root
n = self.k - root.val
if n != root.val:
while node:
if node.val == n: return True
if n > node.val: node = node.right
else: node = node.left
return self.findNumber(root.left) or self.findNumber(root.right)

Python:

class Solution:
def findTarget(self, root, k):
candidates = set()
stack = []
while stack or root:
if root:
val = root.val
if val in candidates:
return True
else:
candidates.add(k - val)
stack.append(root)
root = root.left
else:
node = stack.pop()
root = node.right
return False

C++:

bool findTarget(TreeNode* root, int k) {
unordered_set<int> set;
return dfs(root, set, k);
} bool dfs(TreeNode* root, unordered_set<int>& set, int k){
if(root == NULL)return false;
if(set.count(k - root->val))return true;
set.insert(root->val);
return dfs(root->left, set, k) || dfs(root->right, set, k);
}

C++:

bool findTarget(TreeNode* root, int k) {
vector<int> nums;
inorder(root, nums);
for(int i = 0, j = nums.size()-1; i<j;){
if(nums[i] + nums[j] == k)return true;
(nums[i] + nums[j] < k)? i++ : j--;
}
return false;
} void inorder(TreeNode* root, vector<int>& nums){
if(root == NULL)return;
inorder(root->left, nums);
nums.push_back(root->val);
inorder(root->right, nums);
}

C++:  

bool findTarget(TreeNode* root, int k) {
return dfs(root, root, k);
} bool dfs(TreeNode* root, TreeNode* cur, int k){
if(cur == NULL)return false;
return search(root, cur, k - cur->val) || dfs(root, cur->left, k) || dfs(root, cur->right, k);
} bool search(TreeNode* root, TreeNode *cur, int value){
if(root == NULL)return false;
return (root->val == value) && (root != cur)
|| (root->val < value) && search(root->right, cur, value)
|| (root->val > value) && search(root->left, cur, value);
}

  

  

相似题目:

[LeetCode] 1. Two Sum 两数和

[LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组

[LeetCode] 170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计

最新文章

  1. 【转~】初识贝塞尔曲线(B&#233;zier curve)
  2. MySQL备份之【mydumper 学习】
  3. sqoop的命令行操作
  4. Objective-C的IO流
  5. 转:Cocoa、Foundation、UIKit、Objective-c、XCode、Interface Builder的概念
  6. MVC5笔记【一】
  7. Victor 串口 VCL 控件 - 简单实用, 功能强大的 C++ Builder 串口控件!
  8. ExcelUtil
  9. 不想当程序员的CEO不是好投资人:小米雷军23年前所写代码曝光
  10. Zookeeper增删改查
  11. SLAM+语音机器人DIY系列:(八)高阶拓展——1.miiboo机器人安卓手机APP开发
  12. nginx配置https双向验证(ca机构证书+自签证书)
  13. (十五)qt-tcp
  14. Go语言基础之流程控制
  15. js 的运算
  16. CSS使用position:sticky 实现粘性布局
  17. 2016年蓝桥杯省赛A组c++第9题(逆序串问题)
  18. Ubuntu下codeblocks不能自动缩进的问题
  19. python issubclass 和 isinstance函数
  20. 07-SSH综合案例:前台用户模块:结构创建及注册页面跳转

热门文章

  1. git上传者姓名修改
  2. tcp中设置连接超时
  3. nginx和tomcat配置负载均衡和session同步
  4. forword动作
  5. learning scala zipAll
  6. python 报can&#39;t subtract offset-naive and offset-aware datetimes错误
  7. 64、Spark Streaming:StreamingContext初始化与Receiver启动原理剖析与源码分析
  8. HEXO快速搭建自己的博客
  9. C语言第一篇博客
  10. nbbnbnbnbnb