package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 653. Two Sum IV - Input is a BST
 *
 * 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

 */
public class _653 {

    public static class Solution1 {
        public boolean findTarget(TreeNode root, int k) {
            if (root == null) {
                return false;
            }
            List<Integer> list = new ArrayList<>();
            dfs(root, list);
            for (int i = 0; i < list.size() - 1; i++) {
                for (int j = i + 1; j < list.size(); j++) {
                    if (list.get(i) + list.get(j) == k) {
                        return true;
                    }
                }
            }
            return false;
        }

        private void dfs(TreeNode root, List<Integer> list) {
            list.add(root.val);
            if (root.left != null) {
                dfs(root.left, list);
            }
            if (root.right != null) {
                dfs(root.right, list);
            }
        }
    }
}

@@ -134,383 +134,8 @@
 t;%0A%0A
-/**%0A * 653. Two Sum IV - Input is a BST%0A *%0A * Given a Binary Search Tree and a target number,%0A * return true if there exist two elements in the BST such that their sum is equal to the given target.%0A%0A Example 1:%0A Input:%0A    5%0A   / %5C%0A  3   6%0A / %5C   %5C%0A2   4   7%0A%0ATarget = 9%0AOutput: True%0A%0AExample 2:%0AInput:%0A    5%0A   / %5C%0A  3   6%0A / %5C   %5C%0A2   4   7%0A%0ATarget = 28%0AOutput: False%0A%0A */%0A
 publ

refactor 653