129. 求根到叶子节点数字之和

题目描述

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
For example,
1
/
2 3

The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

代码实现

class Solution {
public:
    int sumNumbers(TreeNode *root) {
        
        if(root==nullptr)
            return 0;
        int sum = 0;
        int preSum = 0;
        sumNumbersCore(root,0,sum);
        return sum;      
    }
    void sumNumbersCore(TreeNode *root,int preSum,int & sum)
    {
        int curSum = 10*preSum +root->val;
        if(root->left==nullptr && root->right==nullptr)
        {
            sum+=curSum;
            return;
        }
        if(root->left!=nullptr)
            sumNumbersCore(root->left,curSum,sum);
        if(root->right!=nullptr)
            sumNumbersCore(root->right,curSum,sum);
        return; 
    }
};

posted on 2021-06-12 09:02  朴素贝叶斯  阅读(26)  评论(0编辑  收藏  举报

导航