Please help me finding wrong in my solution


  • 0
    H

    /**

    • Definition for a binary tree node.

    • public class TreeNode {

    • int val;
      
    • TreeNode left;
      
    • TreeNode right;
      
    • TreeNode(int x) { val = x; }
      
    • }
      */
      public class Solution {
      int target=0;
      public int pathSum(TreeNode root, int sum) {
      if(root==null)
      return 0;
      helper(root,sum);
      pathSum(root.left,sum);
      pathSum(root.right,sum);
      return target;
      }

      public int helper(TreeNode root, int sum){
      if(root == null)
      return 0;
      if(sum==root.val){
      target = target + 1;
      System.out.println(root.val);
      // System.out.println(target);
      }
      pathSum(root.left,sum-root.val);
      pathSum(root.right,sum-root.val);
      return 0;
      }
      }


Log in to reply
 

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