Skip to content

Commit dced939

Browse files
author
Sahil
authored
Update 2-SumBinaryTree.cpp
Added another solution with small variation.
1 parent 1f37237 commit dced939

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

Trees/2-SumBinaryTree.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,89 @@ int Solution::t2Sum(TreeNode* A, int B) {
7878

7979
return 0;
8080
}
81+
82+
/*
83+
Another method if you are unable to get above method, although both of them work the same.
84+
*/
85+
86+
87+
// bool isPairPresent(int K, TreeNode* A, TreeNode* B, stack<TreeNode*> s1, stack<TreeNode*> s2){
88+
// // Base Case
89+
// if(!A)return 0;
90+
91+
// // Go to the extreme left
92+
// while(A){
93+
// s1.push(A);
94+
// A = A ->left;
95+
// }
96+
// // Go to the extreme right
97+
// while(B){
98+
// s2.push(B);
99+
// B = B->right;
100+
// }
101+
102+
// Get the lowest and highest elements.
103+
// A = s1.top(); s1.pop();
104+
// B = s2.top(); s2.pop();
105+
106+
// // Make the extremes as lowest and highest values (coz BST)
107+
// int low = A->val, high = B->val;
108+
// bool b1 = true;
109+
// bool b2 = true;
110+
// // While we don't cross the two pointers
111+
// while(low < high){
112+
// // Return if we have found the sum
113+
// if(low + high == K)return 1;
114+
// // If the sum is less, increase the lower pointer only.
115+
// if(low + high < K){
116+
// b2 = false;
117+
// b1 = true;
118+
// }
119+
// // Else decrement the higher pointer only.
120+
// else {
121+
// b2 = true;
122+
// b1 = false;
123+
// }
124+
// if(b1){
125+
// // If the pointer is not NULL, take it to the extreme left
126+
// // of its right child
127+
// if(A){
128+
// A = A->right;
129+
// while(A){
130+
// s1.push(A);
131+
// A = A->left;
132+
// }
133+
// }
134+
// // Else just pop the top of the stack and make it as the lowest pointer.
135+
// else {
136+
// A = s1.top();
137+
// s1.pop();
138+
// low = A->val;
139+
// }
140+
// }
141+
// // Same as above.
142+
// else if(b2){
143+
144+
// if(B){
145+
// B = B->left;
146+
// while(B){
147+
// s2.push(B);
148+
// B = B->right;
149+
// }
150+
// }else {
151+
// B = s2.top();
152+
// s2.pop();
153+
// high = B->val;
154+
// }
155+
// }
156+
// }
157+
// return 0;
158+
// }
159+
160+
// int Solution::t2Sum(TreeNode* A, int B) {
161+
// stack<TreeNode*> s1, s2;
162+
163+
// TreeNode *head1 = A, *head2 = A;
164+
165+
// return isPairPresent(B, head1, head2, s1, s2);
166+
// }

0 commit comments

Comments
 (0)