zoukankan      html  css  js  c++  java
  • [LeetCode] 653. Two Sum IV

    Given the root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target.

    Example 1:

    Input: root = [5,3,6,2,4,null,7], k = 9
    Output: true
    

    Example 2:

    Input: root = [5,3,6,2,4,null,7], k = 28
    Output: false
    

    Example 3:

    Input: root = [2,1,3], k = 4
    Output: true
    

    Example 4:

    Input: root = [2,1,3], k = 1
    Output: false
    

    Example 5:

    Input: root = [2,1,3], k = 3
    Output: true

    Constraints:

    • The number of nodes in the tree is in the range [1, 104].
    • -104 <= Node.val <= 104
    • root is guaranteed to be a valid binary search tree.
    • -105 <= k <= 105

    两数之和 IV - 输入 BST。

    给定一个二叉搜索树 root 和一个目标结果 k,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true

    这道题不难,这里我提供 BFS 和 DFS 两种做法,两种做法时间空间复杂度都一样。这道题其实还是找两数之和,无非是在一棵树里找。因为遍历的是树所以我们不能排序,但是我们还是可以利用 hashset 来提速。对于当前遇到的节点值 root.val,如果 hashset 里存在另一个值 K - root.val 则返回 true。遍历完整棵树还是找不到的话,则返回 false。

    时间O(n)

    空间O(n)

    BFS实现

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     public boolean findTarget(TreeNode root, int k) {
    12         HashSet<Integer> set = new HashSet<>();
    13         Queue<TreeNode> queue = new LinkedList();
    14         queue.offer(root);
    15         while (!queue.isEmpty()) {
    16             if (queue.peek() != null) {
    17                 TreeNode cur = queue.poll();
    18                 if (set.contains(k - cur.val)) {
    19                     return true;
    20                 }
    21                 set.add(cur.val);
    22                 queue.add(cur.right);
    23                 queue.add(cur.left);
    24             } else {
    25                 queue.poll();
    26             }
    27         }
    28         return false;
    29     }
    30 }

    DFS实现

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 class Solution {
    11     public boolean findTarget(TreeNode root, int k) {
    12         HashSet<Integer> set = new HashSet<>();
    13         return dfs(root, set, k);
    14     }
    15 
    16     private boolean dfs(TreeNode root, HashSet<Integer> set, int k) {
    17         if (root == null) return false;
    18         if (set.contains(k - root.val)) {
    19             return true;
    20         }
    21         set.add(root.val);
    22         return dfs(root.left, set, k) || dfs(root.right, set, k);
    23     }
    24 }

    LeetCode 题目总结

  • 相关阅读:
    Unity NGUI 2D场景添加按钮
    EaseType缓动函数
    在没有网络的情况下用安卓手机和数据线让台式电脑上网
    面向对象编程
    static与C#中的static
    C#基础
    iSensor APP 之 摄像头调试 OV5642
    iSensor APP 之 摄像头调试 OV9655
    USB3.0之高速视频传输测试 双目相机(mt9p031、mt9m001)带宽高达300M测试 配合isensor测试 500万像素15fps
    模拟摄像头解码模块最新测试 TVP5150模块 FPGA+SDRAM+TVP5150+VGA 实现PAL AV输入 VGA视频输出
  • 原文地址:https://www.cnblogs.com/cnoodle/p/15183042.html
Copyright © 2011-2022 走看看