【easy】653. Two Sum IV - Input is a BST
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的更多相关文章
- 【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; 完
- 【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 日期 题目地址:ht ...
- 【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 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- Excel阅读模式/聚光灯开发技术之二 超级逐步录入提示功能开发原理简述—— 隐鹤 / HelloWorld
Excel阅读模式/聚光灯开发技术之二 超级逐步录入提示功能开发原理简述———— 隐鹤 / HelloWorld 1. 引言 自本人第一篇博文“Excel阅读模式/单元格行列指示/聚光灯开发技术要 ...
- NodeJs之EXCEL文件导入导出MongoDB数据库数据
NodeJs之EXCEL文件导入导出MongoDB数据库数据 一,介绍与需求 1.1,介绍 (1),node-xlsx : 基于Node.js解析excel文件数据及生成excel文件. (2),ex ...
- Centos6.5 pppoe-server
[root@localhost network-scripts]# rpm -q rp-pppoepackage rp-pppoe is not installed ----------------- ...
- IO模型、线程模型
五种IO模型介绍和对比 https://juejin.im/post/5bd32b84f265da0ac962e7c9 Linux 的 IO 通信 以及 Reactor 线程模型浅析 https:// ...
- git 学习(4) ----- git rebase
使用git rebase 的前提是多人协作下的分支开发,如果是单人开发,那就没有必要使用它了,这是由git rebase 的作用所决定的,git rebase 有两大作用:一个是与主分支保持同步,一个 ...
- bugku web 头等舱
什么也没有. 不行,他肯定把重要的东西隐藏了起来,首先查看源代码 真的什么也没有 burp抓包,看是不是在头部里,嘿嘿找到了
- codeforces932E Team Work
题目链接:CF932E 由第二类斯特林数知 \[ n^m=\sum_{i=0}^nS(m,i)*i!*\dbinom{n}{i} \] \[ \begin{aligned} \sum_{i=1}^n ...
- jquery 设置 transform/translate 获取 transform/translate 的值
//获取 transform 值 var reg=/matrix.(((-)?([0-9]+.)?\d+([, ]+)?){6})./g; var str= progressUI.css(" ...
- The Preliminary Contest for ICPC China Nanchang National Invitational I. Max answer (单调栈+线段树)
题目链接:https://nanti.jisuanke.com/t/38228 题目大意:一个区间的值等于该区间的和乘以区间的最小值.给出一个含有n个数的序列(序列的值有正有负),找到该序列的区间最大 ...
- 关于使用stanfordcorenlp一直运行不报错的解决方法
一.问题描述: 最近在使用stanfordcorenlp时,遇到了我在运行时代码不报错但同时也没有结果的问题,等了很久也没有出结果.其实是很简单的一个步骤,但却花了好几天的时间都没有成功!网上更多的是 ...