2 lines - DFS - JAVA


  • 0
    J
    public class Solution {
        public boolean findTarget(TreeNode root, int k) {
            return DFS(root, new HashSet(), k);
        }
        
        public boolean DFS(TreeNode current, HashSet<Integer> diff, int target) {
            if(current == null) return false;     
            return diff.contains(current.val) || !diff.add(target - current.val) || DFS(current.left, diff, target) || DFS(current.right, diff, target);
        }
    }
    

Log in to reply
 

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