Leetcode: Sum Root to Leaf Numbers

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.

第一反应是一个相当复杂的东西,权当练习STL了吧。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
#include <unordered_map>
#include <queue>

class Solution {
public:
    int sumNumbers(TreeNode *root) {
        if (root == NULL) {
            return 0;
        }
        
        int sum = 0;
        TreeNode *cur = NULL;
        unordered_map<TreeNode*, int> node_values;
        node_values[root] = root->val;
        queue<TreeNode*> nodes;
        nodes.push(root);
        
        while (!nodes.empty()) {
            cur = nodes.front();
            nodes.pop();
            auto node_value = node_values.find(cur);
            assert(node_value != node_values.end());
            if (cur->left == NULL && cur->right == NULL) {
                sum += node_value->second;
            }
            else {
                if (cur->left != NULL) {
                    node_values[cur->left] = node_value->second * 10 + cur->left->val;
                    nodes.push(cur->left);
                }
                if (cur->right != NULL) {
                    node_values[cur->right] = node_value->second * 10 + cur->right->val;
                    nodes.push(cur->right);
                }
            }
        }
        
        return sum;
    }
};

其实可以用递归:

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

=======================第二次===================

回过头来看看,上面的按层次遍历其实挺好的,虽然可以简化一下。有时候面试人家要求非递归啊。


/**
 * 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 == NULL) {
            return 0;
        }
        
        return sum_util(root, 0);
    }
    
    int sum_util(TreeNode *root, int base) {
        base = base * 10 + root->val;
        if (root->left == NULL && root->right == NULL) {
            return base;
        }
        else if (root->left == NULL) {
            return sum_util(root->right, base);
        }
        else if (root->right == NULL) {
            return sum_util(root->left, base);
        }
        else {
            return sum_util(root->left, base) + sum_util(root->right, base);
        }
    }
};


你可能感兴趣的:(LeetCode,递归)