2017.11.1 LeetCode
来源:互联网 发布:linux info命令 编辑:程序博客网 时间:2024/06/03 18:49
653. Two Sum IV - Input is a BST
Description
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.
题意: 给你个二叉排序树,和一个k值,让你判断这里面是否有两个数的和为k
分析:直接把树上的值递归到数组里,然后Two Pointers ,O(n)的的时间复杂度里求出即可
参考函数
class Solution {public: void getnums(TreeNode* root,vector<int>& nums) { if(!root) return; getnums(root->left,nums); nums.push_back(root->val); getnums(root->right,nums); } bool findk(int k,vector<int>& nums) { int len = nums.size(); int l = 0,r = len-1,sum = 0; while(l < r) { if(nums[l] + nums[r] == k) { return true; } else if(nums[l] + nums[r] > k) { r--; } else { l++; } } return false; } bool findTarget(TreeNode* root, int k) { vector<int> nums; getnums(root,nums); return findk(k,nums); }};
阅读全文
0 0
- 2017.11.1 LeetCode
- Leetcode 1
- leetcode(1)
- leetcode 1
- leetcode 1
- leetcode-1
- leetcode 1
- LeetCode(1)
- leetcode 1
- LeetCode(1)
- LeetCode 1
- leetcode-1
- leetcode(1)
- leetcode-1
- LeetCode (1)
- leetcode-1
- leetcode 1
- leetcode(1)
- Jsonp介绍
- ligerUi框架简单的表格展示
- 自定义加载条+百分比
- shiro学习总结(二)
- 自己创建View和改变View
- 2017.11.1 LeetCode
- form表单以及table表格
- 炒鸡炫酷的安卓动画,值得学习
- <胡策day> 11.1 数学+数学+二分、并查集
- cv2 python 图片的批量处理
- HP ProLiant 服务器进入BIOS 按键总结
- B
- pip安装到用户目录(不需要管理员权限)
- vue重定向