LeetCode 653. Two Sum IV
来源:互联网 发布:永凯软件 编辑:程序博客网 时间:2024/05/16 10:23
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.
Example 1:
Input:
5 / \ 3 6 / \ \2 4 7
Target = 9
Output: True
Example 2:
Input:
5 / \ 3 6 / \ \2 4 7
Target = 28
Output: False
知识点: 二分查找树的中序遍历是有序的。
注意: 可能有负数!!
/** * 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: vector<int> v; void fill_v(TreeNode* root) { if(!root) return; fill_v(root->left); v.push_back(root->val); fill_v(root->right); } bool findTarget(TreeNode* root, int k) { fill_v(root); int left = 0, right = v.size()-1; while(left<right) { if(v[left]+v[right]>k) right--; else if(v[left]+v[right]<k) left++; else return true; } return false; }};
阅读全文
0 0
- leetcode 653. Two Sum IV
- Leetcode 653. Two Sum IV
- 【LeetCode】653. Two Sum IV
- Leetcode 653. Two Sum IV
- 【LeetCode】653. Two Sum IV
- leetcode 653. Two Sum IV
- leetcode 653. Two Sum IV
- [LeetCode]653. Two Sum IV
- leetcode: 653. Two Sum IV
- leetcode 653. Two Sum IV
- LeetCode 653. Two Sum IV
- LeetCode-653. Two Sum IV
- leetcode 653. Two Sum IV
- leetcode 653. Two Sum IV
- leetcode[Two Sum IV
- LeetCode Two Sum IV
- leetcode Two Sum IV
- [Leetcode] Binary tree--653. Two Sum IV
- 递归算法及DFS搜索的应用
- 精读《JS中的内存管理》
- Power Stroke(AE描边插件) v1.0.7.3 官方版下载
- Linux(三) 运行级别
- idea开发工具配置
- LeetCode 653. Two Sum IV
- xshell解决中文乱码
- 洛谷 [P1265] 公路修建
- XFTP远程连接中文乱码
- Linux(四) 用户、用户组
- position:relative
- 【OpenGL】模拟太阳系
- HTML学习06-表格
- 线性结构