【LeetCode】653. Two Sum IV - Input is a BST
2017-09-02 15:47
309 查看
【题目】
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:
Example 2:
【分析】
输入是二叉搜索树,二叉搜索树又叫二叉排序树,是一种有序的树,通过遍历可以得到一个有序数组,得到有序数组之后可以采用两指针法从两端向中间遍历,看是否有两个数的和等于target。时间复杂度为最坏为O(h2),最好为O(h),空间复杂度为O(n),n为节点个数。
【代码】
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void middleTraversal(TreeNode root,List<Integer> nums){
if(null==root) return;
middleTraversal(root.left,nums);
nums.add(root.val);
middleTraversal(root.right,nums);
}
public boolean findTarget(TreeNode root, int k) {
//先中序遍历二叉搜索树,存到list中,然后从list中找
List<Integer> nums=new ArrayList<Integer>();
middleTraversal(root,nums);
int len=nums.size();
int i=0,j=len-1;
while(i<j){
if(nums.get(i)+nums.get(j)==k) return true;
while(i<j && nums.get(i)+nums.get(j)<k) ++i;
while(i<j && nums.get(i)+nums.get(j)>k) --j;
}
return false;
}
}
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
【分析】
输入是二叉搜索树,二叉搜索树又叫二叉排序树,是一种有序的树,通过遍历可以得到一个有序数组,得到有序数组之后可以采用两指针法从两端向中间遍历,看是否有两个数的和等于target。时间复杂度为最坏为O(h2),最好为O(h),空间复杂度为O(n),n为节点个数。
【代码】
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void middleTraversal(TreeNode root,List<Integer> nums){
if(null==root) return;
middleTraversal(root.left,nums);
nums.add(root.val);
middleTraversal(root.right,nums);
}
public boolean findTarget(TreeNode root, int k) {
//先中序遍历二叉搜索树,存到list中,然后从list中找
List<Integer> nums=new ArrayList<Integer>();
middleTraversal(root,nums);
int len=nums.size();
int i=0,j=len-1;
while(i<j){
if(nums.get(i)+nums.get(j)==k) return true;
while(i<j && nums.get(i)+nums.get(j)<k) ++i;
while(i<j && nums.get(i)+nums.get(j)>k) --j;
}
return false;
}
}
相关文章推荐
- [LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
- 【LeetCode】653. Two Sum IV - Input is a BST 解题报告
- python--leetcode653. Two Sum IV - Input is a BST
- LeetCode-653. Two Sum IV - Input is a BST
- [Leetcode] 653. Two Sum IV - Input is a BST 解题报告
- Leetcode 653. Two Sum IV - Input is a BST
- leetcode 653. Two Sum IV - Input is a BST
- leetcode: 653. Two Sum IV - Input is a BST
- LeetCode653. Two Sum IV - Input is a BST
- <easy>LeetCode Problem -- 653. Two Sum IV - Input is a BST
- leetcode 653. Two Sum IV - Input is a BST
- leetcode 653. Two Sum IV - Input is a BST
- LeetCode - 653. Two Sum IV - Input is a BST
- Leetcode: 653. Two Sum IV - Input is a BST
- LeetCode-653. Two Sum IV - Input is a BST
- Leetcode 653. Two Sum IV - Input is a BST 两数相加4 解题报告
- 2017.11.1 LeetCode - 653. Two Sum IV - Input is a BST
- leetcode 653. Two Sum IV - Input is a BST
- leetcode 653. Two Sum IV - Input is a BST 中序遍历 + 深度优先遍历DFS
- Leetcode653. Two Sum IV - Input is a BST