classSolution {// O(N) Time & O(H) SpacepublicbooleanfindTarget(TreeNode root,int k) {Deque<TreeNode> stackL =newLinkedList<TreeNode>(); // iterator 1 that gets next smallest valueDeque<TreeNode> stackR =newLinkedList<TreeNode>(); // iterator 2 that gets next largest valuefor (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)returntrue;elseif (tmpSum < k)for (TreeNode cur =stackL.pop().right; cur !=null; cur =cur.left)stackL.push(cur);elsefor (TreeNode cur =stackR.pop().left; cur !=null; cur =cur.right)stackR.push(cur); }returnfalse; }}