当前位置:网站首页>653. Sum of two IV - input BST
653. Sum of two IV - input BST
2022-04-23 09:09:00 【yitahutu79】
Given a binary search tree root And a target result k, If BST There are two elements in and their sum is equal to the given target result , Then return to true.
Example 1:

Input : root = [5,3,6,2,4,null,7], k = 9
Output : true
Example 2:

Input : root = [5,3,6,2,4,null,7], k = 28
Output : false
Tips :
The range of the number of nodes in a binary tree is [1, 104].
-104 <= Node.val <= 104
root For binary search tree
-105 <= k <= 105
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
int count(struct TreeNode *root) {
if (root == NULL) return 0;
return count(root->left) + count(root->right) + 1;
}
void getItem(struct TreeNode *root, int *ret) {
if (root == NULL) return;
getItem(root->left, ret);
ret[++ret[0]] = root->val;
getItem(root->right, ret);
return ;
}
bool findTarget(struct TreeNode* root, int k){
int n = count(root);
int *ret = (int *)malloc(sizeof(int) * (n + 1));
ret[0] = 0;
getItem(root, ret);
int l = 1, r = ret[0];
while (l < r && ret[l] + ret[r] != k) {
if (ret[l] + ret[r] < k) l++;
else r--;
}
return l < r;
}
版权声明
本文为[yitahutu79]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230904164645.html
边栏推荐
- tsdf +mvs
- The most concerned occupations after 00: civil servants ranked second. What was the first?
- Notes on xctf questions
- Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
- RSA 加密解密签名验签
- LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
- NPM installation yarn
- Node installation
- Pctp test experience sharing
- MySQL small exercise (only suitable for beginners, non beginners are not allowed to enter)
猜你喜欢

Non duplicate data values of two MySQL query tables
![[Luke V0] verification environment 2 - Verification Environment components](/img/aa/820000a52bc493a0734ff0eb76f536.png)
[Luke V0] verification environment 2 - Verification Environment components

Arbre de dépendance de l'emballage des ressources

Production practice elk

Redis Desktop Manager for Mac

Notes d'apprentissage oneflow: de functor à opexprinterpreter

小程序报错:Cannot read property 'currentTarget' of undefined

A must see wechat applet development guide 1 - basic knowledge

npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’

Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
随机推荐
Emuelec compilation summary
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
Node installation
调包求得每个样本的k个邻居
Detailed explanation of delete, truncate and drop principles in MySQL database
[58] length of the last word [leetcode]
A must see wechat applet development guide 1 - basic knowledge
政务中台研究目的建设目标,建设意义,技术创新点,技术效果
Go language self-study series | initialization of golang structure
Harbor enterprise image management system
NPM installation yarn
Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
LGB, XGB, cat, k-fold cross validation
Leetcode-199 - right view of binary tree
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
爬虫使用xpath解析时返回为空,获取不到相应的元素的原因和解决办法
LeetCode_DFS_中等_1254. 统计封闭岛屿的数目
How to protect open source projects from supply chain attacks - Security Design (1)
js 原型链的深入
Download and install bashdb