My solution seems to be wrong i covered all the cases


  • 0
    S

    i don't know where i'm going wrong ,i'm adding value to the answer when i reach the leaf node.
    Here answer is static variable and finally i'm returning it.please tell me where i'm going wrong not just the test case.thanks in advance.

    class Solution {
        public:
            int sum(TreeNode *root,int data)
            {
              static int ans=0;
                if(root==NULL)
          	        return 0;
          	    int val=data*10+root->val;
            	if(root->left ==NULL && root->right ==NULL)
                	ans+=val;
            	else
            	{
                	sum(root->left,val);
                	sum(root->right,val);
            	}
            	return ans;
            }
            int sumNumbers(TreeNode *root) {
                return sum(root,0);   
            }
        };

Log in to reply
 

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