Q129. Sum Root to Leaf Numbers

直达:https://leetcode.com/problems/sum-root-to-leaf-numbers/description/

Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.

An example is the root-to-leaf path1->2->3which represents the number123.

Find the total sum of all root-to-leaf numbers.

For example,

    1
   / \
  2   3

The root-to-leaf path1->2represents the number12. The root-to-leaf path1->3represents the number13.

Return the sum = 12 + 13 =25.

分析

类似于Q112, 不同之处在于去路径之和的时候要对之前的路径乘以10。

C++代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int sumNumbers(TreeNode* root) {
        int res = 0;
        if(!root) return 0;
        helper(root, root->val, res, 0);
        return res;
    }
private:
    void helper(TreeNode* root, int path, int& res, int height){
        if (!root->left && !root->right){
            res += path;
            return;
        }else{
            if(root->left) helper(root->left, 10 * path + root->left->val, res, height+1);
            if(root->right) helper(root->right, 10 * path + root->right->val, res, height+1);
        }
    }
};

Last updated

Was this helpful?