zoukankan      html  css  js  c++  java
  • Leetcode 653 两数之和IV

    Leetcode 653 两数之和IV - 输入BST

    数据结构定义:

    给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true。
    
    案例 1:
    
    输入: 
        5
       / 
      3   6
     /    
    2   4   7
    
    Target = 9
    
    输出: True
     
    
    案例 2:
    
    输入: 
        5
       / 
      3   6
     /    
    2   4   7
    
    Target = 28
    
    输出: False
        
        
    /**
     * 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 {
        private Set<Integer> set = new HashSet<>();
        public boolean findTarget(TreeNode root, int k) {
            if(root == null)
                return false;
            if(set.contains(k - root.val))
                return true;
            set.add(root.val);
            return findTarget(root.left,k) || findTarget(root.right,k);
        }
    }
    

    广度优先遍历方式:

    class Solution {
        public boolean findTarget(TreeNode root, int k) {
            if(root == null)
                return false;
            Set<Integer> set = new HashSet<>();
            Queue<TreeNode> queue = new LinkedList<>();
            queue.offer(root);
            while(!queue.isEmpty()){
                TreeNode node = queue.poll();
                if(set.contains(k - node.val))
                    return true;
                set.add(node.val);
                if(node.left != null)
                    queue.offer(node.left);
                if(node.right != null)
                    queue.offer(node.right);
            }
            return false;
    
        }
    }
    

    中序 + 双指针:

    /*
    * 思路: 先中序遍历出所有的数值,然后双指针比较两数之和
    */
    class Solution {
        private List<Integer> list =new ArrayList<>();
        public boolean findTarget(TreeNode root, int k) {
            if(root == null)
                return false;
            inOrder(root);
            int i = 0,j = list.size()-1;
            while(i < j){
                int sum = list.get(i) + list.get(j);
                if(sum == k)
                    return true;
                else if(sum < k)
                    i++;
                else 
                    j--;
            }
            return false;
            
        }
        private void inOrder(TreeNode root){
            if(root == null)
                return;
            inOrder(root.left);
            list.add(root.val);
            inOrder(root.right);
        }
    }
    

    最优解:

    /*
    * 思路: 每拿到一个节点,就在整棵树种判断是否右相匹配的节点使它们的和等于 k
    */
    class Solution {
        private List<Integer> list =new ArrayList<>();
        public boolean findTarget(TreeNode root, int k) {
            if(root == null)
                return false;
            inOrder(root);
            int i = 0,j = list.size()-1;
            while(i < j){
                int sum = list.get(i) + list.get(j);
                if(sum == k)
                    return true;
                else if(sum < k)
                    i++;
                else 
                    j--;
            }
            return false;
            
        }
        private void inOrder(TreeNode root){
            if(root == null)
                return;
            inOrder(root.left);
            list.add(root.val);
            inOrder(root.right);
        }
    }
    
  • 相关阅读:
    数据库水平切分(拆库拆表)的实现原理解析(转)
    json序列化 & 反序列化
    数据库工作原理
    【原创】python多线程测试接口性能
    XML解析(DOM、ElementTree)及转换为JSON
    nginx+supervisor+gunicorn+flask
    3、爬取干货集中营的福利图片
    Python多环境扩展管理
    九、frp对外提供简单的文件访问服务
    八、frps服务端与nginx可共用80端口
  • 原文地址:https://www.cnblogs.com/CodingXu-jie/p/14117089.html
Copyright © 2011-2022 走看看