a simple but slow java solution


  • 1
    W
    int count = 0;
    int origin;
    public int pathSum(TreeNode root, int sum) {
        origin = sum;
        dfs(root);
        return count;
    }
    private void dfs(TreeNode root) {
    	if(root == null) return;
    	dfsSum(root, origin);
    	dfs(root.left);
    	dfs(root.right);
    }
    
    private void dfsSum(TreeNode root, int sum) {
        if(root == null) return;
        if(sum - root.val == 0) {
            count++;
        }
        dfsSum(root.left, sum - root.val);
        dfsSum(root.right, sum - root.val);
    }

Log in to reply
 

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