notes/OJ notes/pages/Leetcode Two-Sum-IV-Input-Is-a-BST.md

121 lines
2.7 KiB
Markdown
Raw Permalink Normal View History

2022-07-08 11:29:47 +08:00
# Leetcode Two-Sum-IV-Input-Is-a-BST
#### 2022-07-08 11:11
> ##### Algorithms:
2022-09-03 15:41:36 +08:00
>
> #algorithm #binary_search #BFS
>
2022-07-08 11:29:47 +08:00
> ##### Data structures:
2022-09-03 15:41:36 +08:00
>
> #DS #binary_tree #binary_search_tree
>
2022-07-08 11:29:47 +08:00
> ##### Difficulty:
2022-09-03 15:41:36 +08:00
>
2022-09-06 20:22:48 +08:00
> #coding_problem #difficulty_easy
2022-09-03 15:41:36 +08:00
>
2022-07-08 11:29:47 +08:00
> ##### Additional tags:
2022-09-03 15:41:36 +08:00
>
2022-07-08 11:29:47 +08:00
> #leetcode
2022-09-03 15:41:36 +08:00
>
2022-07-08 11:29:47 +08:00
> ##### Revisions:
2022-09-03 15:41:36 +08:00
>
2022-07-08 11:29:47 +08:00
> N/A
##### Related topics:
2022-09-03 15:41:36 +08:00
2022-07-08 11:29:47 +08:00
##### Links:
2022-09-03 15:41:36 +08:00
2022-07-08 11:29:47 +08:00
- [Link to problem](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)
- [Three method to solve this](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/discuss/106059/JavaC%2B%2B-Three-simple-methods-choose-one-you-like)
2022-09-03 15:41:36 +08:00
---
2022-07-08 11:29:47 +08:00
### Problem
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_.
#### Examples
**Example 1:**
![](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg)
**Input:** root = [5,3,6,2,4,null,7], k = 9
**Output:** true
**Example 2:**
![](https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg)
**Input:** root = [5,3,6,2,4,null,7], k = 28
**Output:** false
#### Constraints
2022-09-03 15:41:36 +08:00
- 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`
2022-07-08 11:29:47 +08:00
### Thoughts
> [!summary]
> This is a #BFS #hash_table problem.
Mainly two methods:
2022-09-03 15:41:36 +08:00
2022-07-08 11:29:47 +08:00
1. #BFS with hash table. Time space O(n)
2022-09-03 15:41:36 +08:00
This can be quicker since you are starting at the middle, which is more likely to hit the answer, theoretically taking less time.
2022-07-08 11:29:47 +08:00
2. #binary_search. Time O(hn), h is the height of BST, best case h == log(n), worst case h == n
2022-09-03 15:41:36 +08:00
for every node, binary search in the tree for the answer.
2022-07-08 11:29:47 +08:00
### Solution
BFS with hash table
2022-09-03 15:41:36 +08:00
2022-07-08 11:29:47 +08:00
```cpp
/**
* 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:
bool findTarget(TreeNode* root, int k) {
// BFS with unordered_set
// Take note: when to push root, when to push root->left and root->right
unordered_set<int> uset;
queue<TreeNode*> pending;
pending.push(root);
2022-09-03 15:41:36 +08:00
2022-07-08 11:29:47 +08:00
TreeNode* ptr;
while(!pending.empty()) {
ptr = pending.front();
pending.pop();
2022-09-03 15:41:36 +08:00
2022-07-08 11:29:47 +08:00
// find first, to avoid k = 10, val = 5
if (uset.find(ptr->val) != uset.end()) {
return true;
}
uset.insert(k - ptr->val);
2022-09-03 15:41:36 +08:00
2022-07-08 11:29:47 +08:00
if (ptr->left) {
pending.push(ptr->left);
}
if (ptr->right) {
pending.push(ptr->right);
}
}
2022-09-03 15:41:36 +08:00
2022-07-08 11:29:47 +08:00
return false;
}
};
2022-09-03 15:41:36 +08:00
```