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 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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void dfs(TreeNode* root){
if(root==NULL) return;
nums.push_back(root->val);
checked[root->val]++;
dfs(root->left);
dfs(root->right);
}
bool findTarget(TreeNode* root, int k) {
dfs(root);
for(int i=0;i<nums.size();i++)
if(k-nums[i]!=nums[i]&&checked[k-nums[i]]>=1)
return true;
else if(k-nums[i]==nums[i]&&checked[nums[i]]>=2)
return true;
return false;
}
private:
vector<int> nums;
map<int,int> checked;
};
LeetCode 653. Two Sum IV – Input is a BST的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- 【Leetcode_easy】653. Two Sum IV - Input is a BST
problem 653. Two Sum IV - Input is a BST 参考 1. Leetcode_easy_653. Two Sum IV - Input is a BST; 完
- 【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 日期 题目地址:ht ...
- [LeetCode&Python] Problem 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 ...
- 【leetcode】653. Two Sum IV - Input is a BST
Given the root of a Binary Search Tree and a target number k, return true if there exist two element ...
- 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 ...
随机推荐
- 如何在Linux上升级java
首先使用rpm -qa|grep gcj命令查找安装信息 卸载老版java: rpm -e <检索到软件名> 下载最新java JDK: 自行到oracle官网下载相应的版本,放到linu ...
- 60分钟课程: 用egg.js实现增删改查,文件上传和restfulApi, webpack react es6 (一)
今天开始我将写nodejs框架egg.js, react 实现的增删改查,文件上传等常用的b/s场景,这个将分3部分来写. 会让你在60分钟内快速 入口并应用~ 你应该用es6, node,或是ph ...
- ubuntu下sublime屏蔽alt键显示顶部菜单栏
在sulime下喜欢把方向键映的上下左右映射为alt+i/k/j/l ,按单词移动alt+h/; 但是在ubuntu下按alt会弹出sublime的顶部菜单栏,如果想屏蔽sublime按alt屏蔽菜单 ...
- CF1060C Maximum Subrectangle
思路: 不难发现,对矩阵中的数字求和实际上是先分别对a,b两个数列中对应子段的元素求和再相乘.题目是要求在和不超过给定值的情况下使选出的矩阵面积最大.我们反其道而行之,考虑在子段长度一定的情况下,和最 ...
- poj2135 最小费用流
添加超级源点(与点1之间的边容量为2,权值为0)和超级汇点(与点N之间的边容量为2,权值为0),求流量为2的最小费用流.注意是双向边. #include <iostream> #inclu ...
- Android镜像文件ramdisk.img,system.img,userdata.img介绍
Android 源码编译后,在out目录下生成的三个镜像文件:ramdisk.img,system.img,userdata.img以及它们对应的目录树root,system,data. ramdis ...
- CSS布局之-高度自适应
何为高度自适应? 高度自适应就是高度能跟随浏览器窗口的大小改变而改变,典型的运用在一些后台界面中上面一栏高度固定用作菜单栏或导航栏,下面一栏高度自适应用于显示内容.高度自适应不像宽度自适应那样简单,在 ...
- js 字符串常用操作
function Class_String_Common(){ //将字符串 myString 的 start位置 和 end位置 之间的内容替换为 replaceStr this.replace1 ...
- ubuntu 14.04 安装tomcat服务器 配置图片路径和文件路径
root@hett-PowerEdge-T30:/usr/local/src# lltotal 235956drwxr-xr-x 6 root root 4096 3月 26 14:48 ...
- 系统相册中获取gif图片 保证取到的图片不会改变
NSURL *imageRefURL = [info valueForKey:UIImagePickerControllerReferenceURL]; ...