-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1214.cpp
More file actions
76 lines (59 loc) · 1.66 KB
/
Copy path1214.cpp
File metadata and controls
76 lines (59 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
1214. Two Sum BSTs
Medium
171
15
Add to List
Share
Given two binary search trees, return True if and only if there is a node in the first tree and a node in the second tree whose values sum up to a given integer target.
Example 1:
Input: root1 = [2,1,4], root2 = [1,0,3], target = 5
Output: true
Explanation: 2 and 3 sum up to 5.
Example 2:
Input: root1 = [0,-10,10], root2 = [5,1,7,0,2], target = 18
Output: false
Constraints:
Each tree has at most 5000 nodes.
-10^9 <= target, node.val <= 10^9
*/
/**
* 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 twoSumBSTs(TreeNode* root1, TreeNode* root2, int target) {
if(!root1 || !root2)
return false;
stack<TreeNode*> st;
unordered_set<int> ust;
while(!st.empty() || root1 != NULL){
while(root1){
st.push(root1);
root1 = root1->left;
}
root1 = st.top();st.pop();
ust.insert(target - root1->val);
root1 = root1->right;
}
while(!st.empty() || root2 != NULL){
while(root2){
st.push(root2);
root2 = root2->left;
}
root2 = st.top();st.pop();
if(ust.count(root2->val))
return true;
root2 = root2->right;
}
return false;
}
};