【easy】653. Two Sum IV - Input is a BST
BST树求得两个节点的和是target
//因为是BST所以中序遍历得到的是递增数组
//这个题的方法就是在一个递增数组中找到两个数的和相加是目标结果
/**
* 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:
bool findTarget(TreeNode* root, int k) {
vector<int> nums;
stack<TreeNode*> nodes;
//写BST树的中序遍历,用到一个stack
while (!nodes.empty() || root){
if (root){
nodes.push(root);
root = root->left;
}
else{
root = nodes.top();
nodes.pop();
nums.push_back(root->val);
root = root->right;
}
}
//处理nums数组找到两个数的和是目标结果数
int left = ;
int right = nums.size()-;
while (left<right){
int sum = nums[left] + nums[right];
if (sum == k){
return true;
}
else if (sum < k)
left ++;
else
right --;
}
return false;
}
};
【easy】653. Two Sum IV - Input is a BST的更多相关文章
- 【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】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 ...
- 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] 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 ...
- 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&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 ...
- 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 ...
随机推荐
- SSZipArchive的使用详解和遇到的问题
https://blog.csdn.net/zhengang007/article/details/51019479 2016年03月30日 版权声明:本文为博主原创文章,转载请注明作者和原文链接. ...
- LVS实现负载均衡原理
负载均衡集群是load balance 集群的简写.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均衡设备有F5.NetsNetscale.这里主要是学习lvs. === ...
- bs4模块
1.导入模块 from bs4 import BeautifulSoup 2.创建对象 Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它 ...
- 数据库MySQL——初识
认识数据库—MySQL 楔子 假设现在你已经是某大型互联网公司的高级程序员,让你写一个火车票购票系统,来hold住十一期间全国的购票需求,你怎么写? 由于在同一时段抢票的人数太多,所以你的程序不可能写 ...
- c#调试快捷键
F5 - 启动调试 Shift + F5 - 停止调试 F9 - 设置断点 Ctrl + Shift + F9 - 删除所有断点 F10 - 逐过程调试 F11 - 逐语句调试 Ctrl+R+E: ...
- 使用zabbix监控nginx的活动连接数
使用zabbix监控nginx的活动连接数 1.方法简述 zabbix可以自定义很多监控,只要是能通过命令获取到相关的值,就可以在zabbix的监控中增加该对象进行监控,在zabbix中,该对象称之为 ...
- Java第五周学习总结
学号 2016-2017-2 <Java程序设计>第X周学习总结 教材学习内容总结 1.接口 (1)使用关键字interface来定义一个接口,接口分为接口声明和接口体,例如 interf ...
- EC20 R2.1
1.模块开机成功前WAKEUP_IN. NET_MODE. BT_CTS. COEX_UART_TX(背部焊盘). COEX_UART_RX(背部焊盘) 和WLAN_EN(背部焊盘)引脚禁止上拉. 2 ...
- .NET常用开发框架汇总
分布式缓存框架:Microsoft Velocity:微软自家分布式缓存服务框架.Memcahed:一套分布式的高速缓存系统,目前被许多网站使用以提升网站的访问速度.Redis:是一个高性能的KV数据 ...
- windows10安装docker,运行jhipster-registry
1.官网下载windows版docker 2.设置bios 3.CMD进入到某个jhipster的工程目录,执行"docker-compose -f src/main/docker/jhip ...