leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0653.cpp (661B)


      1 class Solution {
      2     TreeNode *root;
      3     TreeNode *find(int k) {
      4         TreeNode *t = root;
      5         while (t && t->val != k)
      6             t = t->val > k ? t->left : t->right;
      7         return t;
      8     }
      9 
     10   public:
     11     bool findTarget(TreeNode *root, int k) {
     12         stack<TreeNode *> st;
     13         st.push(this->root = root);
     14         while (!st.empty()) {
     15             TreeNode *t, *root = st.top();
     16             st.pop();
     17             while (root) {
     18                 if ((t = find(k - root->val)) && t != root) return true;
     19                 if (root->right) st.push(root->right);
     20                 root = root->left;
     21             }
     22         }
     23         return false;
     24     }
     25 };