class Solution {
// O(N) Time & O(H) Space
public boolean findTarget(TreeNode root, int k) {
Deque<TreeNode> stackL = new LinkedList<TreeNode>(); // iterator 1 that gets next smallest value
Deque<TreeNode> stackR = new LinkedList<TreeNode>(); // iterator 2 that gets next largest value
for (TreeNode cur = root; cur != null; cur = cur.left)
stackL.push(cur);
for (TreeNode cur = root; cur != null; cur = cur.right)
stackR.push(cur);
while (stackL.size() != 0 && stackR.size() != 0 && stackL.peek() != stackR.peek()) {
int tmpSum = stackL.peek().val + stackR.peek().val;
if (tmpSum == k)
return true;
else if (tmpSum < k)
for (TreeNode cur = stackL.pop().right; cur != null; cur = cur.left)
stackL.push(cur);
else
for (TreeNode cur = stackR.pop().left; cur != null; cur = cur.right)
stackR.push(cur);
}
return false;
}
}