Accepted Java Solution Nested Recursion

  • 0
    public class Solution {
        int total;
        public int pathSum(TreeNode root, int sum) {
            total = 0;
            traverse(root, sum);
            return total;
        public void traverse(TreeNode root, int sum){
            if(root == null) return;
            traverse(root.left, sum);
            traverse(root.right, sum);
        public void helper(TreeNode root, int v, int sum){
            if(root == null) return;
            v += root.val;
            if(v==sum) total++;
            helper(root.left, v, sum);
            helper(root.right, v, sum);

Log in to reply

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