package leetcode;

/**
 * https://leetcode.com/problems/two-sum-iv-input-is-a-bst/
 */
public class Problem653 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public boolean findTarget(TreeNode root, int k) {
        // TODO
        return false;
    }

    public static void main(String[] args) {
        Problem653 prob = new Problem653();

        TreeNode root = new TreeNode(5);
        root.left = new TreeNode(3);
        root.right = new TreeNode(6);
        root.left.left = new TreeNode(2);
        root.left.right = new TreeNode(4);
        root.right.right = new TreeNode(7);

        System.out.println(prob.findTarget(root, 9)); // true
        System.out.println(prob.findTarget(root, 28)); // false
    }
}

@@ -12,16 +12,65 @@
 tcode;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -385,506 +385,868 @@
     
-// TODO%0A        return false;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem653 prob = new Problem653();%0A%0A        TreeNode root = new TreeNode(5);%0A        root.left = new TreeNode(3);%0A        root.right = new TreeNode(6);%0A        root.left.left = new TreeNode(2);%0A        root.left.right = new TreeNode(4);%0A        root.right.righ
+Map%3CInteger, Integer%3E map = new HashMap%3C%3E();%0A        findTarget(root, map);%0A        for (Map.Entry%3CInteger, Integer%3E entry : map.entrySet()) %7B%0A            int val = entry.getKey();%0A            Integer count = map.get(k - val);%0A            if (count != null) %7B%0A                if (val == k - val && count %3E= 2) %7B%0A                    return true;%0A                %7D else if (val != k - val) %7B%0A                    return true;%0A                %7D%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A    private static void findTarget(TreeNode root, Map%3CInteger, Integer%3E map) %7B%0A        if (roo
 t =
+=
  n
-ew TreeNode(7);%0A%0A        System.out.println(prob.findTarget(root, 9)); // true%0A        System.out.println(prob.findTarget(root, 28)); // false
+ull) %7B%0A            return;%0A        %7D%0A        if (!map.containsKey(root.val)) %7B%0A            map.put(root.val, 1);%0A        %7D else %7B%0A            map.put(root.val, map.get(root.val) + 1);%0A        %7D%0A        findTarget(root.left, map);%0A        findTarget(root.right, map);
 %0A   

Solve problem 653