Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.
Example 1:
Input:
5
/ \
3 6
/ \ \
2 4 7 Target = 9 Output: True
Example 2:
Input:
5
/ \
3 6
/ \ \
2 4 7 Target = 28 Output: False
Idea 1. Similar to Two Sum LT1, use set to record the element while looping the BST and check if target - num exists.
Time complexity: O(n)
Space complexity: O(h) + O(n)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private boolean findHelper(TreeNode root, int k, Set<Integer> record) {
if(root == null) {
return false;
} if(record.contains(k - root.val)) {
return true;
}
record.add(root.val);
return findHelper(root.left, k, record)
|| findHelper(root.right, k, record);
}
public boolean findTarget(TreeNode root, int k) {
return findHelper(root, k, new HashSet<Integer>());
}
}
Idea 2 BFS + Set, insted of DFS(preorder, inorder, postordal) traversal like idea 1
Time complexity: O(n)
Space complexity: O(n)
class Solution {
public boolean findTarget(TreeNode root, int k) {
if(root == null) {
return false;
}
Deque<TreeNode> queue = new ArrayDeque<>();
Set<Integer> record = new HashSet<>();
queue.add(root); while(!queue.isEmpty()) {
TreeNode node = queue.pollFirst();
if(record.contains(k - node.val)) {
return true;
}
record.add(node.val);
if(node.left != null) {
queue.offerLast(node.left);
}
if(node.right != null) {
queue.offerLast(node.right);
}
} return false;
}
}
Idea 3. Store the ordered nums in array after inorder traversal, then two pointer scannning towards each other.
Time complexity: O(n)
Space complexity: O(h) + O(n)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private void inorderWalk(TreeNode root, List<Integer> nums) {
if(root == null) {
return;
} inorderWalk(root.left, nums);
nums.add(root.val);
inorderWalk(root.right, nums);
} public boolean findTarget(TreeNode root, int k) {
List<Integer> nums = new ArrayList<>(); inorderWalk(root, nums); for(int left = 0, right = nums.size()-1; left < right; ) {
int sum = nums.get(left) + nums.get(right);
if(sum == k) {
return true;
}
else if(sum < k) {
++left;
}
else {
--right;
}
} return false;
}
}
Idea 4. Inorder walk iterator + reversed inorder walk iterator to avoid extra space.
巧点: 1. 怎么停止循环?利用bst有序不重复,left < right; 否则 iter.hasNext()
2. 只有需要时才移动iterator
3. next() -> 遍历到下一个数就返回
Time complexity: O(n)
Space complexity: O(h)
import java.util.NoSuchElementException;
import java.lang.UnsupportedOperationException;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ class InorderIterator implements Iterator<TreeNode> {
private TreeNode curr;
private Deque<TreeNode> stack;
private boolean forward; public InorderIterator(TreeNode root, boolean forward) {
this.curr = root;
this.stack = new ArrayDeque<>();
this.forward = forward;
} public boolean hasNext() {
return curr!= null || !stack.isEmpty();
} public TreeNode next() {
if(!hasNext()) {
throw new NoSuchElementException("tree run out of elements");
}
while(curr != null) {
stack.push(curr);
if(forward) {
curr = curr.left;
}
else {
curr = curr.right;
}
}
curr = stack.pop();
TreeNode prev = curr;
if(forward) {
curr = curr.right;
}
else {
curr = curr.left;
}
return prev;
} public void remove() {
throw new UnsupportedOperationException();
}
} class Solution {
public boolean findTarget(TreeNode root, int k) {
InorderIterator iter = new InorderIterator(root, true);
InorderIterator reversedIter = new InorderIterator(root, false); for(int left = iter.next().val, right = reversedIter.next().val; left < right;) {
if(left + right == k) {
return true;
}
else if(left + right < k) {
left = iter.next().val;
}
else {
right = reversedIter.next().val;
}
} return false;
}
}
import java.util.NoSuchElementException;
import java.lang.UnsupportedOperationException;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ class InorderIterator implements Iterator<TreeNode> {
private TreeNode curr;
private Deque<TreeNode> stack;
private boolean forward; public InorderIterator(TreeNode root, boolean forward) {
this.curr = root;
this.stack = new ArrayDeque<>();
this.forward = forward;
} public boolean hasNext() {
return curr!= null || !stack.isEmpty();
} public TreeNode next() {
// if(!hasNext()) {
// throw new NoSuchElementException("tree run out of elements");
// }
while(curr != null || !stack.isEmpty()) {
if(curr != null) {
stack.push(curr);
if(forward) {
curr = curr.left;
}
else {
curr = curr.right;
}
}
else {
curr = stack.pop();
TreeNode prev = curr;
if(forward) {
curr = curr.right;
}
else {
curr = curr.left;
}
return prev;
}
}
throw new NoSuchElementException("tree run out of elements");
} public void remove() {
throw new UnsupportedOperationException();
}
} class Solution {
public boolean findTarget(TreeNode root, int k) {
InorderIterator forwardIter = new InorderIterator(root, true);
InorderIterator backwardIter = new InorderIterator(root, false); for(int left = forwardIter.next().val, right = backwardIter.next().val; left < right; ) {
int sum = left + right;
if(sum == k) {
return true;
}
else if (sum < k) {
left = forwardIter.next().val;
}
else {
right = backwardIter.next().val;
} } return false;
}
}