[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 中存在两个元素且它们的和等于给定 ...
随机推荐
- 获取dmp文件的schema
白天的时候,做了一个获取dmp文件的schema实验,特此记录一下. 参考文章:如何获取dmp文件的schema -- by 我的烟灰缸 http://oradb.cc/2017/07/10/%E5 ...
- fail2ban 防止ssh暴力破解
1.环境 CentOS 7 2.在线安装 yum install -y epel-release yum install -y fail2ban fail2ban 结构 /etc/fail2ban ...
- CSS速查列表-2-(text)文本
CSS的Text属性可以改变页面中 1.文本的颜色(color). 2.字符间距(word-spacing ) 属性可以改变字(单词)之间的标准间隔.其默认值 normal 与设置值为 0 是一样的 ...
- Linux下的 >, >>, <, ps, |, grep, /dev/null
1 要将命令行运行的结果保存到文件中,truncate模式下使用 >,append模式下使用 >> ls > ~/test.txt 2 要将文件中的内容作为标准输入,应使用 & ...
- <经验杂谈>C#使用AES加密解密的简单介绍
AES 算法是基于置换和代替的.置换是数据的重新排列,而代替是用一个单元数据替换另一个.AES 使用了几种不同的技术来实现置换和替换. 以下是我自己用c#研究出来算法Code: /// <sum ...
- SpringMVC之处理流程
之前在学servlet时写过JavaWeb与Asp.net工作原理比较分析,那篇主要是大致描述了下servlet的工作流程,今天在家了解了下springmvc的工作原理,与asp.net中的mvc进行 ...
- 读论文系列:Object Detection CVPR2016 YOLO
CVPR2016: You Only Look Once:Unified, Real-Time Object Detection 转载请注明作者:梦里茶 YOLO,You Only Look Once ...
- Beta冲刺 第四天
Beta冲刺 第四天 1. 昨天的困难 1.网页使用了一些网上现成的模板,其主要是使用像素做处理的,所以检查起来比较费事费力. 2.使用github代码merge时出现了问题.所以花费了不少的时间在人 ...
- python的Virtualenv
Virtualenv 虚拟的 Python 环境(简称 venv) 是一个能帮助你在本地目录安装不同版本的 Python 模块的 Python 环境,你可以不再需要在你系统中安装所有东西就能开发并测试 ...
- initializer element is not a compile-time constant
初始化一个全局变量或static变量时,只能用常量赋值,不能用变量赋值! 如下就会报这个错误(KUIScreenWidth)是变量 static CGFloat const topButtonWidt ...