• LeetCode-653.Two Sum IV


    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 boolean findTarget(TreeNode root, int k) {
            List<Integer> list = new ArrayList<Integer>();
            midList(root,list);
            int left =0;
            int right =list.size()-1;
            while(left<right){
                int sum =list.get(left)+list.get(right);
                if(sum==k){
                    return true;
                }
                else if(sum<k){
                    left++;
                }
                else{
                    right--;
                }
            }
            return false;
        }
        
        public void midList(TreeNode root,List<Integer > list){
            if(root==null){
                return;
            }
            midList(root.left,list);
            list.add(root.val);
            midList(root.right,list);
        }
  • 相关阅读:
    web漏洞之SQL注入
    web漏洞之文件包含
    web漏洞之文件上传
    CVE-2020-1938 Apache-Tomcat-Ajp漏洞复现
    web漏洞之命令执行
    web漏洞之XXE
    web漏洞之CORS
    web漏洞之SSRF
    web漏洞之CSRF
    web漏洞之XSS
  • 原文地址:https://www.cnblogs.com/zhacai/p/11311890.html
Copyright © 2020-2023  润新知