[leetcode-653-Two Sum IV - Input is a BST]

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

思路:

中序遍历二叉树,将节点值保存在数组里面。然后用从头和尾部遍历刚刚得到的有序数组。

void middle( TreeNode* root, vector< int >& a )
{
    if( root == NULL )  return;
    middle( root->left, a );
    a.push_back( root->val );
    middle( root->right, a );
}
bool findTarget(TreeNode* root, int k)
{
    vector< int > a;
    middle( root, a );

    int n = a.size();
    for( int i = 0, j = n-1; i < j;  )
    {
        if( a[i] + a[j] < k )
            i++;
        else if( a[i] + a[j] > k )
            j--;
        else
            return true;
    }
    return false;
}
时间: 2024-08-05 13:36:07

[leetcode-653-Two Sum IV - Input is a BST]的相关文章

LeetCode 653. Two Sum IV - Input is a BST(在BST中寻找两个节点,使它们的和为一个给定值)

题意:在BST中寻找两个节点,使它们的和为一个给定值. /** * 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 inor

653. Two Sum IV - Input is a BST 两个数的和,输入为平衡二叉树

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 Targe

653. Two Sum IV - Input is a BST

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 Targe

[LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树

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 Targe

[Leetcode] Binary tree--653. Two Sum IV - Input is a BST

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 Targe

[LeetCode] Two Sum IV - Input is a BST

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 Targe

653. Two Sum IV - Input is a BST-easy

我不懂有没有收藏之类的功能,收藏别人的解法. tql,不懂为什么直接比较set里的值,不是两个数sum么 有一些答案都用到了iterator迭代器 http://www.cplusplus.com/reference/iterator/ 我的思路是Just solve it as 2-sum problem using two pointers. 但是不懂重载的两个操作符 Need to implement BSTiterator class first, which overloads two

Two Sum IV - Input is a BST

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 Targe

LeetCode 167. Two Sum II - Input array is sorted (两数之和之二 - 输入的是有序数组)

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 m