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. Windows系统下的TCP参数优化(注册表\TCPIP\Parameters)

    转自:https://blog.csdn.net/libaineu2004/article/details/49054261 Windows系统下的TCP参数优化   TCP连接的状态与关闭方式及其对 ...

  2. Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'

    from django.db import models # Create your models here. class Category(models.Model): caption = mode ...

  3. Linux centos ssh

    创建m01.backup.nfs.web01.web02 m01(172.16.1.61).backup(172.16.1.41).nfs(172.16.1.31).web01(172.16.1.7) ...

  4. lazyMan

    class Lazyman { constructor() { this.tasks = []; this.init(); } init() { const task = () => { con ...

  5. Struts2+Spring+Hibernate3整合

    这几天正在复习三大框架的知识,特意把写出来,如有错误,希望大家多指教! 代码地址:https://git.coding.net/puchenglin/SSHDemo.git 1. 引入jar包 Str ...

  6. odoo10源码 windows环境安装

    odoo10-windows开发环境(源码安装) 安装node 下载地址 http://nodejs.cn/download/ 安装教程https://www.cnblogs.com/zhouyu20 ...

  7. 【CSA49F】【XSY3317】card 博弈论 DP

    题目大意 不会博弈论的 yww 在和博弈论大师 yxq 玩一个游戏. 有 \(n\) 种卡牌,第 \(i\) 种卡牌有 \(b_i\) 张. yww 会先把所有 \(B=\sum_{i=1}^nb_i ...

  8. Linux-文件管理

    文件管理 创建.复制.删除.移动.查看.编辑.压缩.查找 Linux目录结构 Windows: 以多根的方式组织文件 C:\ D:\ E:\Linux: 以单根的方式组织文件 / /目录结构: FSH ...

  9. MySQL物理备份 lvm-snapshot

    MySQL备份之 lvm-snapshot lvm-snapshot(工具备份) 优点: 几乎是热备(穿件快照前把表上锁,创建完成后立即释放) 支持所有引擎 备份速度快 无需使用昂贵的商业软件(它是操 ...

  10. Django websocket 长连接使用

    下载  pip install dwebsocket WebSocket是一种在单个TCP连接上进行全双工通信的协议 WebSocket使得客户端和服务器之间的数据交换变得更加简单,允许服务端主动向客 ...