leet-code/src/main/java/leetcode/editor/cn/TwoSumIvInputIsABst.java

84 lines
1.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给定一个二叉搜索树 root 和一个目标结果 k如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true。
//
//
//
// 示例 1
//
//
//输入: root = [5,3,6,2,4,null,7], k = 9
//输出: true
//
//
// 示例 2
//
//
//输入: root = [5,3,6,2,4,null,7], k = 28
//输出: false
//
//
//
//
// 提示:
//
//
// 二叉树的节点个数的范围是 [1, 10⁴].
// -10⁴ <= Node.val <= 10⁴
// root 为二叉搜索树
// -10⁵ <= k <= 10⁵
//
// Related Topics 树 深度优先搜索 广度优先搜索 二叉搜索树 哈希表 双指针 二叉树 👍 373 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.TreeNode;
import java.util.ArrayList;
import java.util.List;
//653:两数之和 IV - 输入 BST
public class TwoSumIvInputIsABst {
public static void main(String[] args) {
Solution solution = new TwoSumIvInputIsABst().new Solution();
}
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean findTarget(TreeNode root, int k) {
if (root == null) {
return false;
}
if (list.contains(k - root.val)) {
return true;
}
list.add(root.val);
if (findTarget(root.left, k)) {
return true;
}
if (findTarget(root.right, k)) {
return true;
}
return false;
}
List<Integer> list = new ArrayList<>();
}
//leetcode submit region end(Prohibit modification and deletion)
}