title: Return a Node having maximum sum of itself and its children in the entire tree but the max sum is being updated even if the current sum is less
body: <p>Need to find the node with the maximum sum of children and itself. The Input format :</p>
  <p>Line 1 : Elements in level order form separated by space (as per done in class). Order is -</p>
  <p>Root_data, n (No_Of_Child_Of_Root), n children, and so on for every element</p>
  <p>Output format : Node with maximum sum.</p>
  <p>It returns correct answers but if you run the code by uncommenting the print statement you can see that the max sum is altered even though the current sum is less than the max sum.</p>
  <pre><code>public class Solution {
  
  /*  TreeNode structure 
   * 
   * class TreeNode&lt;T&gt; {
      T data;
      ArrayList&lt;TreeNode&lt;T&gt;&gt; children;
  
      TreeNode(T data){
          this.data = data;
          children = new ArrayList&lt;TreeNode&lt;T&gt;&gt;();
      }
  }*/
  
  
  
  
  public static TreeNode&lt;Integer&gt; maxSumNode(TreeNode&lt;Integer&gt; root){
          // Write your code here
          return maxSumNode(root, 0);
      }
      
      public static TreeNode&lt;Integer&gt; maxSumNode(TreeNode&lt;Integer&gt; root, int maxSum){
      
      if(root.children.size() == 0){
          return null;
          
      }
      
      TreeNode&lt;Integer&gt; maxNode = null;
      int tempSum = root.data;
      for(int i = 0 ; i &lt; root.children.size() ; ++i){
          tempSum += root.children.get(i).data;
      }
      if(tempSum &gt; maxSum){
          maxSum = tempSum;
          maxNode = root;
          
      }
      //System.out.println(&quot;MaxNum now for &quot;+root.data+&quot; is: &quot;+ maxSum);
      
      for(int i = 0 ; i &lt; root.children.size() ; ++i){
          TreeNode&lt;Integer&gt; temp = maxSumNode(root.children.get(i), maxSum);
          if(temp == null){
              continue;
          }
          if(temp != null){
              maxNode = temp;
          }
      }
      return maxNode;
  }
  }
  </code></pre>
  <p>for example if i give the input:
  5 3 1 2 3 1 15 2 4 5 1 6 0 0 0 0</p>
  <p>I get the output correctly but the maxSum is weird:</p>
  <p><a href="https://i.stack.imgur.com/Oi1es.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Oi1es.png" alt="Result" /></a></p>