<p>Given the <code>root</code> of a binary search tree and an integer <code>k</code>, return <code>true</code> <em>if there exist two elements in the BST such that their sum is equal to</em> <code>k</code>, <em>or</em> <code>false</code> <em>otherwise</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg" style="width: 400px; height: 229px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], k = 9
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg" style="width: 400px; height: 229px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,7], k = 28
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li>
	<li><code>-10<sup>4</sup> &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
	<li><code>root</code> is guaranteed to be a <strong>valid</strong> binary search tree.</li>
	<li><code>-10<sup>5</sup> &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>

// /**
//  * 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:
//     void solve(int& k, stack<TreeNode*> &nxt, stack<TreeNode*>& bfr,bool& ans){
//         if(nxt.empty() || bfr.empty())    return ;
//         TreeNode* a=nxt.top(),*b=bfr.top();
//         if(a==b)  return;
//         int sum =a->val+b->val;
//         cout<<sum<<" ";
//         if(sum==k){ ans=true;  return;}
//         if(sum<k){
//             TreeNode* tmp=a;
//             nxt.pop();
//             tmp=tmp->right;
//             while(tmp){
//                 nxt.push(tmp);
//                 tmp=tmp->left;
//             }
//         }else{
//             TreeNode* tmp=b;
//             bfr.pop();
//             tmp=tmp->left;
//             while(tmp){
//                 nxt.push(tmp);
//                 tmp=tmp->right;
//             }
//         }
//         solve(k,nxt,bfr,ans);
//     }
//     bool findTarget(TreeNode* root, int k) {
//         stack<TreeNode*> nxt, bfr;
//         TreeNode* tmp =root;
//         while(root){
//             nxt.push(root);
//             root=root->left;
//         }
//         while(tmp){
//             bfr.push(tmp);
//             tmp=tmp->right;
//         }
//         bool ans=0;
//         solve(k,nxt,bfr,ans);
//         return ans;
//     }
// };
class Solution {
public:
    void solve(int& k, stack<TreeNode*>& nxt, stack<TreeNode*>& bfr, bool& ans) {
        if (nxt.empty() || bfr.empty()) return;

        TreeNode* a = nxt.top();
        TreeNode* b = bfr.top();

        // Prevent iterators from overlapping
        if (a->val >= b->val) return;

        int sum = a->val + b->val;
        cout << sum << " "; // Debug output

        if (sum == k) {
            ans = true;
            return;
        }

        if (sum < k) {
            nxt.pop();
            TreeNode* tmp = a->right;
            while (tmp) {
                nxt.push(tmp);
                tmp = tmp->left;
            }
        } else {
            bfr.pop();
            TreeNode* tmp = b->left;
            while (tmp) {
                bfr.push(tmp);
                tmp = tmp->right;
            }
        }

        solve(k, nxt, bfr, ans);
    }

    bool findTarget(TreeNode* root, int k) {
        if (!root) return false;

        stack<TreeNode*> nxt, bfr;

        TreeNode* tmp = root;
        while (tmp) {
            nxt.push(tmp);
            tmp = tmp->left;
        }

        tmp = root;
        while (tmp) {
            bfr.push(tmp);
            tmp = tmp->right;
        }

        bool ans = false;
        solve(k, nxt, bfr, ans);
        return ans;
    }
};
40400