当前位置:网站首页>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
边栏推荐
- 【原创】使用System.Text.Json对Json字符串进行格式化
- Latex mathematical formula
- Flink SQL realizes the integration of stream and batch
- Colorui solves the problem of blocking content in bottom navigation
- I don't understand time, timestamp and time zone. Look at this article
- Non duplicate data values of two MySQL query tables
- Enterprise wechat application authorization / silent login
- Rembg split mask
- 员工试用期转正申请书(泸州老窖)
- Production practice elk
猜你喜欢
Valgrind et kcachegrind utilisent l'analyse d'exécution
Notes on xctf questions
论文阅读《Multi-View Depth Estimation by Fusing Single-View Depth Probability with Multi-View Geometry》
Notes d'apprentissage oneflow: de functor à opexprinterpreter
LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
Project upload part
BK3633 规格书
Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
Consensus Token:web3. 0 super entrance of ecological flow
搞不懂时间、时间戳、时区,快来看这篇
随机推荐
[indexof] [lastIndexOf] [split] [substring] usage details
valgrind和kcachegrind使用运行分析
考研线性代数常见概念、问题总结
Whether the same binary search tree (25 points)
Brief steps to build a website / application using flash and H5
Correct method of calculating inference time of neural network
Single chip microcomputer nixie tube stopwatch
Flink SQL realizes the integration of stream and batch
First principle mind map
Error: cannot find or load main class
Notes on xctf questions
Talent Plan 学习营初体验:交流+坚持 开源协作课程学习的不二路径
Node installation
OpenCV中的图像处理 —— 轮廓入门+轮廓特征
資源打包關系依賴樹
共享办公室,提升入驻体验
What is augmented reality technology? Where can it be used?
Automatic differentiation and higher order derivative in deep learning framework
小女孩行走
Go language self-study series | golang structure pointer