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. 菜鸟学IT之python3关于列表,元组,字典,集合浅认识!

    作业来源:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2753 一.列表,元组,字典,集合分别如何增删改查及遍历. 列表 # 列表的 ...

  2. 证明与计算(2): 离散对数问题(Discrete logarithm Problem, DLP)

    离散对数问题,英文是Discrete logarithm Problem,有时候简写为Discrete log,该问题是十几个开放数学问题(Open Problems in Mathematics, ...

  3. 小程序ios开发注意点

    两个月了啊,这两个月完成了一个vue的项目还有一个小程序,终于可以休息一下了, 今天先声明一个奇怪的bug,在我开发微信小程序的时候, 发现有个获取商品详情的接口在安卓手机上是可以获取数据的, 但是i ...

  4. POJ1847 Tram

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 20274   Accepted: 7553 Description ...

  5. 利用bootstrap-select.min.js实现bootstrap下拉列表的单选和多选

    参考文章:https://blog.csdn.net/qq_37677519/article/details/78143522

  6. springdata jpa查询用like时候需要输入该属性的类型字节码

  7. MDK 编译错误和警告 使用时遇到的小问题

    main.c(32): warning:  #1-D: last line of file ends without a newline 这个是由于在main函数的“}”后,没有加回车. 只要在mai ...

  8. pixy&STM32使用记录(串口&SPI外设)

    先踏踏实实的把stm32的外设串口,SPI搞清楚,不要眼高手低,看不起小事.用SPI通信将pixy的数据读出来,将数据用串口发到串口助手上,然后处理数据,利用STM32的定时器调节pwm,控制电机,先 ...

  9. jQuery中$.each()方法(遍历)

    $.each()是对数组,json和dom结构等的遍历,说一下他的使用方法吧. 1.遍历一维数组 var arr1=['aa','bb','cc','dd']; $.each(arr1,functio ...

  10. IIS搭建校内小站

    背景 在学校读书最大的烦恼是去机房忘了带作业,或者带了U盘传文件又很容易丢,或者的或者用学校内网网盘又容易忘了上传. 所以笔者搞了个办法. IIS准备工作 win+pause break 打开控制面板 ...