8ms c++


  • 0
    B
    class Solution {
    public:
        int ans = 0;
        int sumNumbers(TreeNode* root) {
            if (root==NULL) return 0;
            depthSearch(root,0);
            return ans;
        }
        void depthSearch(TreeNode* root, int number)
        {
            number = number * 10 +root->val;
            if (root->left == NULL && root->right == NULL)
            {
                ans += number;
                return ;
            }
            if (root->left!=NULL) depthSearch(root->left,number);
            if (root->right!=NULL) depthSearch(root->right,number);
        }
    };
    

Log in to reply
 

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