tanyagupta0201 / Hacktoberfest-2022

This repository has been excluded from the Hacktoberfest 2022. You may use other Repo for your valuable contributions
https://github.com/tanyagupta0201/LeetCode-Problems-Solutions
41 stars 98 forks source link

Create TWOSUMINABST.cpp #75

Closed devanshikapla closed 1 year ago

devanshikapla commented 1 year ago

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; } };