- Two Sum IV - Input is a BST
Given the root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target.
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
Constraints:
The number of nodes in the tree is in the range [1, 104].
-104 <= Node.val <= 104
root is guaranteed to be a valid binary search tree.
-105 <= k <= 105
solution1 DFS
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
unordered_set<int>set;
bool findTarget(TreeNode* root, int k) {
if(root==nullptr) return false;
if(set.count(k- root->val)) return true;
set.insert(root->val);
return findTarget(root->left,k) || findTarget(root->right,k);
}
};
solution2 BFS
class Solution {
public:
bool findTarget(TreeNode *root, int k) {
unordered_set<int> hashTable;
queue<TreeNode *> que;
que.push(root);
while (!que.empty()) {
TreeNode *node = que.front();
que.pop();
if (hashTable.count(k - node->val)) {
return true;
}
hashTable.insert(node->val);
if (node->left != nullptr) {
que.push(node->left);
}
if (node->right != nullptr) {
que.push(node->right);
}
}
return false;
}
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/two-sum-iv-input-is-a-bst/solution/liang-shu-zhi-he-iv-shu-ru-bst-by-leetco-b4nl/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
solution3 DFS+LDR
class Solution {
public:
vector<int> vec;
void inorderTraversal(TreeNode *node) {
if (node == nullptr) {
return;
}
inorderTraversal(node->left);
vec.push_back(node->val);
inorderTraversal(node->right);
}
bool findTarget(TreeNode *root, int k) {
inorderTraversal(root);
int left = 0, right = vec.size() - 1;
while (left < right) {
if (vec[left] + vec[right] == k) {
return true;
}
if (vec[left] + vec[right] < k) {
left++;
} else {
right--;
}
}
return false;
}
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/two-sum-iv-input-is-a-bst/solution/liang-shu-zhi-he-iv-shu-ru-bst-by-leetco-b4nl/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
solution4 iteration+DFS
class Solution {
public:
TreeNode *getLeft(stack<TreeNode *> &stk) {
TreeNode *root = stk.top();
stk.pop();
TreeNode *node = root->right;
while (node != nullptr) {
stk.push(node);
node = node->left;
}
return root;
}
TreeNode *getRight(stack<TreeNode *> &stk) {
TreeNode *root = stk.top();
stk.pop();
TreeNode *node = root->left;
while (node != nullptr) {
stk.push(node);
node = node->right;
}
return root;
}
bool findTarget(TreeNode *root, int k) {
TreeNode *left = root, *right = root;
stack<TreeNode *> leftStack, rightStack;
leftStack.push(left);
while (left->left != nullptr) {
leftStack.push(left->left);
left = left->left;
}
rightStack.push(right);
while (right->right != nullptr) {
rightStack.push(right->right);
right = right->right;
}
while (left != right) {
if (left->val + right->val == k) {
return true;
}
if (left->val + right->val < k) {
left = getLeft(leftStack);
} else {
right = getRight(rightStack);
}
}
return false;
}
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/two-sum-iv-input-is-a-bst/solution/liang-shu-zhi-he-iv-shu-ru-bst-by-leetco-b4nl/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。