title: tree: root to leaf sum (recursion)
body: <p>the problem is: calculate the total sum of all root-to-leaf numbers. for example: if the tree is (1,2,3), 1 is root, 2 is left child, 3 is right child, two paths: 1->2 1->3, sum=12+13=25</p>
  
  <p>this is my correct recursive solution. in the helper method, return the sum:</p>
  
  <pre><code>public int sumNumbers(TreeNode root) {
      if (root == null) {
          return 0;
      }
      return getSum(root, 0);
  }
  private int getSum(TreeNode root, int value) {
      if (root.left == null &amp;&amp; root.right == null) {
          return root.val + value * 10;
      }
      int sum = 0;
      if (root.left != null) {
          sum += getSum(root.left, value * 10 + root.val);
      }
      if (root.right != null) {
          sum += getSum(root.right, value * 10 + root.val);
      }
      return sum;
  }
  </code></pre>
  
  <p>but when I add the <code>sum</code> as a parameter in the helper method, I always got 0. </p>
  
  <pre><code>public int getSum(TreeNode root) {
      int sum = 0, path = 0;
      helper(root, path, sum);
      return sum;
  }
  private void helper(TreeNode root, int path, int sum) {
      if (root == null) {
          return;
      }
      int path = 10 * path + root.val;
      if (root.left == null &amp;&amp; root.right == null) {
          sum += path;
          return;
      }
      helper(root.left, path, sum);
      helper(root.right, path, sum);
  }
  </code></pre>
  
  <p>I believe there must be some points I misunderstand about the recursion. thank you in advance to give me some explanation why the value of <code>sum</code> is not 'transferred' back to the <code>sum</code> in <code>getSum</code> method. </p>