给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true。

struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
}; class Solution {
public:
TreeNode* node;
bool findTarget(TreeNode* root, int k) {
if(root == NULL)
return false;
node = root;
return GetAns(root, k);
} bool GetAns(TreeNode* root, int k)
{
if(root == NULL)
return false;
int temp = k - root ->val;
TreeNode* t = Search(node, temp);
if(t != NULL && t != root)
return true;
return GetAns(root ->left, k) || GetAns(root ->right, k);
} TreeNode* Search(TreeNode* root, int k)
{
if(root == NULL)
return NULL;
if(root ->val == k)
return root;
else if(root ->val > k)
return Search(root ->left, k);
else return Search(root ->right, k);
}
};

Leetcode653.Two Sum IV - Input is a BST两数之和4-输入BST的更多相关文章

  1. 167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(i ...

  2. [LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

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

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  4. [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  5. [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  6. 167. Two Sum II - Input array is sorted两数之和

    1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...

  7. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  8. LeetCode 167:两数之和 II - 输入有序数组 Two Sum II - Input array is sorted

    公众号: 爱写bug(ID:icodebugs) 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index ...

  9. LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

随机推荐

  1. Js中的onblur和onfocus事件应用介绍

    html页面中,诸如按钮.文本框等可视元素都具有拥有和失去焦点的事件,本文以文本框获得和失去焦点为例简单讲解onfocus和onblur的应用 html页面中,诸如按钮.文本框等可视元素都具有拥有和失 ...

  2. Myeclipse 10使用hibernate生成注解(annotation)实体类

    以MySQL数据库为例,请在数据库里面建好对应的表. 1.配置数据库链接 打开Myelipse Database Explorer视图 Window-->Open Perspective--&g ...

  3. Spring框架使用ByName自动注入同名问题剖析

    问题描述   我们在使用spring框架进行项目开发的时候,为了配置Bean的方便经常会使用到Spring当中的Autosire机制,Autowire根据注入规则的不同又可以分为==ByName==和 ...

  4. myeclipse14 破解教程

        myeclipse14 破解教程     注意:先不要打开myeclipse,破解完成之后再打开    Myeclipse-2014-GA-破解文件  链接: https://pan.baid ...

  5. 用Axure RP7创建响应式原型教程

    自从几年前响应式技术开始应用时,创建响应式原型就成为了很多人苦恼的事情.响应式设计用一种非常优雅的方式处理为多种设备类型使用HTML和CSS编码的应用,但是提供给UX专业人士的原型工具却没有具备同样品 ...

  6. rpm包安装过程中依赖问题“libc.so.6 is needed by XXX”解决方法-转

    原文:http://raksmart.idcspy.com/781 在CentOS上的Canon LBP2900安装打印机驱动,中间遇到了一些问题,主要是安装rpm包出现的依赖问题,现在解决了,现在简 ...

  7. HDU--2126 Buy the souvenirs(二维01背包)

    题目http://acm.hdu.edu.cn/showproblem.php?pid=2126 分析:有两个要求,一是计算最多可以选多少中纪念品:而是计算选最多纪念品的方案有多少种, 即统计最优方案 ...

  8. Leetcode95. Unique Binary Search Trees II不同的二叉搜索树2

    给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [   [1,null,3,2],   [3,2,null,1],   [3,1,null,nul ...

  9. parameter -- tWR

    http://www.samsung.com/global/business/semiconductor/file/product/tWR-0.pdf tWR: write recovery time ...

  10. vue.js_12_vue的watch和computed

    1.watch用来监测指定Vue实例上的数据变动. watch主要用于监控vue实例的变化,它监控的变量当然必须在data里面声明才可以,它可以监控一个变量,也可以是一个对象. 1.>使用wat ...