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的更多相关文章

  1. 【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; 完

  2. 【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 日期 题目地址:ht ...

  3. 【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 ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. 播放包含flash内容的网页或flash内容, 无法显示相应flash内容

    问题描述 通过Messenger发布的html5网页到player, 如下图所示: 布局播放效果: 解决办法 从Cnario Player菜单栏打开Setting>>Canvas Cont ...

  2. [2019BUAA人工智能实战_陈泽寅]第1次个人作业

    我们的征程是星辰和大海 一.索引 项目 内容 这个作业属于哪个课程 BUAA人工智能实战 这个作业的要求在哪里 here 我在这个课程的目标是 理论实践相结合,提升自己coding能力 这个作业在哪个 ...

  3. html 通用导航 a链接跳转时给当前导航添加选中颜色

    学习前端的同学或许会遇到这个问题 做一个基本的小站有几个导航的,如下图 无论有几个页面,这里的导航的样式都是一样,唯一不同的就是进入哪个页面时当前有个选中的样式 一般这样通用的导航在开发的时候都会封装 ...

  4. Nginx HTTP框架提供的请求相关变量

    L73 binary_remote_addr 对端二进制IPV4或IPV6 一般用作限制用户请求缓存key connection 递增链接序号 connection_requests  一条TCP链接 ...

  5. v-for 循环element-ui菜单

    vue 使用了element-ui的菜单组件, 这个组件的el-menu-item项上,有一个属性index,值是字符串类型, 在使用v-for的index时,它是一个数值型,所以如果直接写index ...

  6. 小计:Shopee批量删除修复~附脚本

    需求 昨天浪的时候,无意之间看到文职人员在一个个删除违禁商品,大概23个店铺,每个店铺500多个商品,页面是用Ajax异步加载的,每删一个就需要等几秒,粗略估计一下用时:9h左右 然后了解了下是什么情 ...

  7. echarts Map(地图) 不同颜色区块显示

    以河南地图为例: 代码如下: <h3>天翼日必达完成率</h3> <div id="map" style="height:340px; te ...

  8. Alertmanager 集群

    Alertmanager 集群搭建 环境准备:2台主机 (centos 7) 192.168.31.151 192.168.31.144 1.安装部署 192.168.31.151 cd /usr/l ...

  9. Python的优势及应用领域

    Python的优势 Python是一门解释型语言,是比较容易入门. Python的程序代码更接近英语,更好好理解. Python的扩展库非常丰富. Python与C的粘合性非常好. Python的缺点 ...

  10. font-spider问题【已解决】

    最近写一个项目,使用了引入的字体,然而字体太大,于是找解决方法,想要把字体压缩一下,然后找到了font-spider;font-spider使用方法这里就不多说了,网上一大把,主要是在node里面安装 ...