[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 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
这道题又是一道2sum的变种题,博主一直强调,平生不识TwoSum,刷尽LeetCode也枉然!只要是两数之和的题,一定要记得先尝试用HashSet来做,这道题只不过是把数组变成了一棵二叉树而已,换汤不换药,我们遍历二叉树就行,然后用一个HashSet,在递归函数函数中,如果node为空,返回false。如果k减去当前结点值在HashSet中存在,直接返回true;否则就将当前结点值加入HashSet,然后对左右子结点分别调用递归函数并且或起来返回即可,参见代码如下:
解法一:
class Solution {
public:
bool findTarget(TreeNode* root, int k) {
unordered_set<int> st;
return helper(root, k, st);
}
bool helper(TreeNode* node, int k, unordered_set<int>& st) {
if (!node) return false;
if (st.count(k - node->val)) return true;
st.insert(node->val);
return helper(node->left, k, st) || helper(node->right, k, st);
}
};
我们也可以用层序遍历来做,这样就是迭代的写法了,但是利用HashSet的精髓还是没变的,参见代码如下:
解法二:
class Solution {
public:
bool findTarget(TreeNode* root, int k) {
if (!root) return false;
unordered_set<int> st;
queue<TreeNode*> q{{root}};
while (!q.empty()) {
auto t = q.front(); q.pop();
if (st.count(k - t->val)) return true;
st.insert(t->val);
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
return false;
}
};
由于输入是一棵二叉搜索树,那么我们可以先用中序遍历得到一个有序数组,然后在有序数组中找两数之和就很简单了,直接用双指针进行遍历即可,参见代码如下:
解法三:
class Solution {
public:
bool findTarget(TreeNode* root, int k) {
vector<int> nums;
inorder(root, nums);
for (int i = , j = (int)nums.size() - ; i < j;) {
if (nums[i] + nums[j] == k) return true;
(nums[i] + nums[j] < k) ? ++i : --j;
}
return false;
}
void inorder(TreeNode* node, vector<int>& nums) {
if (!node) return;
inorder(node->left, nums);
nums.push_back(node->val);
inorder(node->right, nums);
}
};
类似题目:
Two Sum III - Data structure design
Two Sum II - Input array is sorted
参考资料:
https://leetcode.com/problems/two-sum-iv-input-is-a-bst/
https://leetcode.com/problems/two-sum-iv-input-is-a-bst/discuss/106090/my-c-python-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 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 ...
- Leetcode653.Two Sum IV - Input is a BST两数之和4-输入BST
给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true. struct TreeNode { int val; struct TreeNode * ...
- [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 ...
- [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 ...
- 167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(i ...
- 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 ...
- 167. Two Sum II - Input array is sorted两数之和
1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...
- 【leetcode 简单】第三十八题 两数之和 II - 输入有序数组
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值( ...
- LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)
653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定 ...
随机推荐
- iOS 跑马灯带图片可点击
项目中有个需求,需要以跑马灯的形势滚动展示用户的实时数据,跑马灯需要有用户头像,内容的长度不固定,并且可以点击,滚动效果还要足够流畅,本着不重复造轮子的心理,在网上各种搜索,发现都没法找到满足需求的d ...
- sql的优化30条
1. 对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2. 应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使 ...
- [开源] yvm - 自制Java虚拟机
项目地址 : https://github.com/racaljk/yvm 虚拟机现在已可运行(不过还有很多待发现待修复的bugs),已支持语言特性有: Java基本算术运算,流程控制语句,面向对象. ...
- bug终结者 团队作业第二周
bug终结者 团队作业第二周 我们小组选取游戏"开心消消乐",回答问题: 1. 此类软件是什么时候开始出现的, 这些软件是怎么说服你(陌生人)成为他们的用户的? 他们的目标都是盈利 ...
- bug终结者 团队作业第四、五周
bug终结者 团队作业第四.五周 博客编辑者:20162322朱娅霖 本周学习任务: 团队协作完成<需求规格说明书> 工作流程 第四周 团队成员各自完成蓝墨云班课中<需求规格说明书& ...
- mongodb 定时备份
通过centos 脚步来执行备份操作,使用crontab实现定时功能,并删除指定天数前的备份 具体操作: 1.创建Mongodb数据库备份目录 mkdir -p /home/backup/mongod ...
- JAVA_SE基础——11.Java中的运算符
在程序设计中,运算符应用得十分广泛,通过运算符可以将两个变量进行任意运算.数学中的"+"."-"."*"."/"运算符同 ...
- wpf研究之道——datagrid控件分页
这是我们的datagrid分页效果图,有上一页,下一页,可以跳到任何一页.当页码比较多的时候,只显示几页,其余用点点,界面实现如下: <!--分页--> <StackPanel Or ...
- istio入门(01)istio是什么?
- Linux实战案例(6)yum查找、卸载、和安装软件
0.查找要安装的软件名字 yum search iostat就能查到以及iostat相干的安装包了, 别的想安装一个程序,只记得一部门名称,也可以用这个措施来实现安装 yum search png | ...