653:两数之和 IV - 输入 BST
This commit is contained in:
parent
194eeb8957
commit
d443e0e0c7
84
src/main/java/leetcode/editor/cn/TwoSumIvInputIsABst.java
Normal file
84
src/main/java/leetcode/editor/cn/TwoSumIvInputIsABst.java
Normal file
@ -0,0 +1,84 @@
|
||||
//给定一个二叉搜索树 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)
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
<p>给定一个二叉搜索树 <code>root</code> 和一个目标结果 <code>k</code>,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 <code>true</code>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/sum_tree_1.jpg" style="height: 229px; width: 400px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong> root = [5,3,6,2,4,null,7], k = 9
|
||||
<strong>输出:</strong> true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/21/sum_tree_2.jpg" style="height: 229px; width: 400px;" />
|
||||
<pre>
|
||||
<strong>输入:</strong> root = [5,3,6,2,4,null,7], k = 28
|
||||
<strong>输出:</strong> false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>二叉树的节点个数的范围是 <code>[1, 10<sup>4</sup>]</code>.</li>
|
||||
<li><code>-10<sup>4</sup> <= Node.val <= 10<sup>4</sup></code></li>
|
||||
<li><code>root</code> 为二叉搜索树</li>
|
||||
<li><code>-10<sup>5</sup> <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>树</li><li>深度优先搜索</li><li>广度优先搜索</li><li>二叉搜索树</li><li>哈希表</li><li>双指针</li><li>二叉树</li></div></div><br><div><li>👍 373</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user