美文网首页
[刷题防痴呆] 0653 - 两数之和IV - 输入BST (T

[刷题防痴呆] 0653 - 两数之和IV - 输入BST (T

作者: 西出玉门东望长安 | 来源:发表于2022-02-15 00:12 被阅读0次

    题目地址

    https://leetcode.com/problems/two-sum-iv-input-is-a-bst/

    题目描述

    653. Two Sum IV - Input is a BST
    
    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
    
    

    思路

    • 由于是BST, 所以inorder的traverse就是从小到大排好序的list. 在有序list上用双指针找两数之和即可.
    • DFS + SET.
    • BFS + SET.

    关键点

    代码

    • 语言支持:Java
    /**
     * 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;
     *     }
     * }
     */
    
    
    // inorder, two pointer
    class Solution {
        public boolean findTarget(TreeNode root, int k) {
            if (root == null) {
                return false;
            }
            List<Integer> list = new ArrayList<>();
            inorder(list, root);
            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;
        }
    
        private void inorder(List<Integer> list, TreeNode node) {
            if (node == null) {
                return;
            }
            inorder(list, node.left);
            list.add(node.val);
            inorder(list, node.right);
        }
    }
    
    // dfs + set
    class Solution {
        Set<Integer> set = new HashSet<>();
        boolean res = false;
        public boolean findTarget(TreeNode root, int k) {
            if (root == null) {
                return false;
            }
            dfs(root, k);
            return res;
        }
    
        private void dfs(TreeNode node, int k) {
            if (node == null) {
                return;
            }
            if (set.contains(k - node.val)) {
                res = true;
            } else {
                set.add(node.val);
                dfs(node.left, k);
                dfs(node.right, k);
            }
        }
    }
    
    // BFS + set
    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()) {
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    TreeNode cur = queue.poll();
                    if (set.contains(k - cur.val)) {
                        return true;
                    }
                    set.add(cur.val);
                    if (cur.left != null) {
                        queue.offer(cur.left);
                    }
                    if (cur.right != null) {
                        queue.offer(cur.right);
                    }
                }
            }
            return false;
        }
    }
    

    相关文章

      网友评论

          本文标题:[刷题防痴呆] 0653 - 两数之和IV - 输入BST (T

          本文链接:https://www.haomeiwen.com/subject/yxnghrtx.html