-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode_653.cpp
More file actions
173 lines (149 loc) · 5.01 KB
/
Leetcode_653.cpp
File metadata and controls
173 lines (149 loc) · 5.01 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// 653. Two Sum IV - Input is a BST
/*
TIME COMPLEXITY: O(n) where n is the number of nodes in the tree
- Morris traversal takes O(n) time to create a sorted array
- Two-pointer approach takes O(n) time in the worst case
- Overall time complexity is O(n)
SPACE COMPLEXITY: O(n)
- We store all node values in a vector
- Morris traversal uses O(1) extra space, but the vector requires O(n) space
*/
/*
Given the root of a binary search tree and an integer k,
return true if there exist two elements in the BST such that their sum is equal to k,
or false otherwise.
Example 1:
Input: root = [5,3,6,2,4,null,7], k = 9
Output: true
Explanation: 5 + 4 = 9
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, 10^4].
- -10^4 <= Node.val <= 10^4
- root is guaranteed to be a valid binary search tree.
- -10^5 <= k <= 10^5
*/
#include <iostream>
#include <vector>
using namespace std;
// 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) {
if (!root) return false;
// Step 1: Perform Morris Inorder Traversal to get sorted values
vector<int> values;
TreeNode* current = root;
while (current) {
if (!current->left) {
values.push_back(current->val);
current = current->right;
} else {
// Find the inorder predecessor
TreeNode* predecessor = current->left;
while (predecessor->right && predecessor->right != current) {
predecessor = predecessor->right;
}
if (!predecessor->right) {
// Set right pointer to current (temporary link)
predecessor->right = current;
current = current->left;
} else {
// Revert the temporary link
predecessor->right = nullptr;
values.push_back(current->val);
current = current->right;
}
}
}
// Step 2: Use two pointers to find if two numbers sum to k
int left = 0;
int right = values.size() - 1;
while (left < right) {
int sum = values[left] + values[right];
if (sum == k) {
return true;
} else if (sum < k) {
left++;
} else {
right--;
}
}
return false;
}
};
// Helper function to create a binary tree from an array (for testing)
TreeNode* createTree(const vector<int>& values, int index = 0) {
if (index >= values.size() || values[index] == -1) {
return nullptr;
}
TreeNode* root = new TreeNode(values[index]);
root->left = createTree(values, 2 * index + 1);
root->right = createTree(values, 2 * index + 2);
return root;
}
// Helper function to delete the tree and free memory
void deleteTree(TreeNode* root) {
if (!root) return;
deleteTree(root->left);
deleteTree(root->right);
delete root;
}
// Test function
void runTests() {
Solution solution;
// Test case 1
{
vector<int> values = {5, 3, 6, 2, 4, -1, 7};
TreeNode* root = createTree(values);
int k = 9;
bool result = solution.findTarget(root, k);
cout << "Test 1: " << (result ? "PASSED" : "FAILED")
<< " (Expected: true, Got: " << (result ? "true" : "false") << ")\n";
deleteTree(root);
}
// Test case 2
{
vector<int> values = {5, 3, 6, 2, 4, -1, 7};
TreeNode* root = createTree(values);
int k = 28;
bool result = solution.findTarget(root, k);
cout << "Test 2: " << (!result ? "PASSED" : "FAILED")
<< " (Expected: false, Got: " << (result ? "true" : "false") << ")\n";
deleteTree(root);
}
// Test case 3: Single node
{
TreeNode* root = new TreeNode(1);
int k = 2;
bool result = solution.findTarget(root, k);
cout << "Test 3: " << (!result ? "PASSED" : "FAILED")
<< " (Expected: false, Got: " << (result ? "true" : "false") << ")\n";
delete root;
}
// Test case 4: Two nodes that sum to target
{
TreeNode* root = new TreeNode(2, new TreeNode(1), new TreeNode(3));
int k = 4;
bool result = solution.findTarget(root, k);
cout << "Test 4: " << (result ? "PASSED" : "FAILED")
<< " (Expected: true, Got: " << (result ? "true" : "false") << ")\n";
delete root->left;
delete root->right;
delete root;
}
}
int main() {
runTests();
return 0;
}