Hacktoberfest-2022 icon indicating copy to clipboard operation
Hacktoberfest-2022 copied to clipboard

Create TWOSUMINABST.cpp

Open devanshikapla opened this issue 3 years ago • 0 comments

class Solution { public: bool findTarget(TreeNode* root, int k) { vectorans; stack<TreeNode*>st; TreeNode* Node=root; while(true){ if(Node!=NULL){ st.push(Node); Node=Node->left; } else{ if(st.empty()==true)break; Node=st.top(); st.pop(); ans.push_back(Node->val); Node=Node->right; } } int i=0; int n=ans.size(); int j=n-1; while(i < j){ if(ans[i] + ans[j] == k){ return true; break; } else if(ans[i] + ans[j] < k){ i++; //return false; } else{ j--; //return false; } } return false; } };

devanshikapla avatar Oct 01 '22 07:10 devanshikapla