Java every easy to understand solution


  • 0
    F
    private int count = 0;
    public int pathSum(TreeNode root, int sum) {
    	if(root==null)
    		return 0;
    	rootSum(root, sum, 0);
    	pathSum(root.left, sum);
    	pathSum(root.right, sum);
    	return count;
    }
    private void rootSum(TreeNode root, int sum, int value) {
    	if (root == null)
    		return;
    	if (root.val + value == sum) {
    		count++;
    	}
    	rootSum(root.left, sum, root.val+value);
    	rootSum(root.right, sum, root.val+value);
    }

Log in to reply
 

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