My O(n) accepted concise Java solution


  • 0
    R
        HashSet<Integer> set = new HashSet<Integer>();
        public boolean findTarget(TreeNode root, int k) {
            if(root == null) return false;
            
            boolean left = findTarget(root.left, k);
            
            if(set.contains(k-root.val))
                return true;
            else set.add(root.val);
            
            boolean right = findTarget(root.right,k);
            
            return left || right;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.