<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) {}
 * };
 */

/*
    var minFirst;
    
    void traverse(TreeNode* node) {
1:    TreeNode *x = node->left, *y = node->right;
      if (!minFirst) swap(x, y);
2:    if (x) dfs(x);
3:    output(node);
4:    if (y) dfs(y);
5:  }  
*/

typedef tuple<TreeNode*, TreeNode*, TreeNode*, int> Context; // node, x, y, pc

class Iterator {
private:
  stack<Context> callstack;
  bool minFirst;
  TreeNode* nextNode;
  
  void move() {
    nextNode = NULL;
    
    while (!callstack.empty()) {
      auto& [node, x, y, pc] = callstack.top();
      assert(1 <= pc && pc <= 5);
      
      switch (pc) {
        case 1:
          x = node->left;
          y = node->right;
          if (!minFirst) swap(x, y);
          ++pc;
          break;
          
        case 2:
          if (x) callstack.push(make_tuple(x, (TreeNode*) NULL, (TreeNode*) NULL, 1));
          ++pc;
          break;
          
        case 3:
          nextNode = node;
          ++pc;
          return;
          
        case 4:
          if (y) callstack.push(make_tuple(y, (TreeNode*) NULL, (TreeNode*) NULL, 1));
          ++pc;
          break;
          
        case 5:
          callstack.pop();
          break;
      }
    }
  }
  
public:
  Iterator(TreeNode* root, bool minFirst) : minFirst(minFirst) {
    callstack.push(make_tuple(root, (TreeNode*) NULL, (TreeNode*) NULL, 1));
    move();
  }
  
  TreeNode* next() {
    TreeNode* tmp = nextNode;
    move();
    return tmp;
  }
  
  bool hasNext() {
    return nextNode != NULL;
  }
};

class Solution {
public:
  bool findTarget(TreeNode* root, int k) {
    Iterator i(root, true), j(root, false);
    
    TreeNode *x = i.next(), *y = j.next();
    
    while (true) {
      if (x->val + y->val == k && x != y) return true;
        
      if (x->val + y->val <= k) {
        if (!i.hasNext()) break;
        x = i.next();
      }
      else {
        if (!j.hasNext()) break;
        y = j.next();
      }
    }
    
    return false;
  }
};
38300