Code gives correct answer '1' when run in local dev env but fails with O/P '901' in OJ for test case {0,1}


  • 1
    R
    public class Solution {
        private static int sum=0;
    	public int sumNumbers(TreeNode root) {
    		
    		int sumAtNode=0;
    		if (root==null)
    			return 0;
    		
            else
            {
            	int sumLeft=0;
            	
            	int sumRight=0;
            	
            	sum=sum*10+root.val;
            	sumAtNode=sum;
            	if (root.left !=null)
            	{
            		
            		sumLeft=sumNumbers(root.left);
            		sum=sumAtNode;
            	}
            	if (root.right !=null)
            	{
            		sumRight=sumNumbers(root.right);
            	}
            	if (root.left==null && root.right==null)
        		{
        			return sumAtNode;
        		}
            
            	return sumLeft+sumRight;
            }
        }
    }
    

    kindly help.


Log in to reply
 

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