您的位置:首页 > 其它

129. Sum Root to Leaf Numbers

2016-07-30 15:02 225 查看
题目:根到叶子节点数字之和

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
.

题意:

给定一个节点只包含0到9之间数字的二叉树,每一条根节点到叶子节点的路径代表一个数。

例如,一条根节点到叶子节点路径为1->2->3,则代表的数字为123。

找到所有根节点到叶子节点所组成的数字之和。

思路一:

这道求根到叶节点数字之和的题跟之前的求Path Sum 二叉树的路径和很类似,都是利用DFS递归来解,这道题由于不是单纯的把各个节点的数字相加,而是每到一个新的数字,要把原来的数字扩大10倍之后再相加。

代码:C++版:4ms

/**
 * 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) {
        return dfs(root, 0);
    }
private:
    int dfs(TreeNode *root, int sum) {
        if (root == nullptr) return 0;
        if (root->left == nullptr && root->right == nullptr) //判断是否是叶子节点
            return sum*10 + root->val;
        return dfs(root->left, sum*10 + root->val) + dfs(root->right, sum*10 + root->val);
    }
};

思路二:

非递归实现,借助栈实现对遍历过得节点进行存储,以方便节点寻找上一级节点,先找到最左边路径的数,之后寻找右节点,及其他节点。

代码:C++版:4ms

/**
 * 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) {
        stack<TreeNode*> nodes;
        if (!root) return 0;
        
        int total = 0;
        int current = 0;
        TreeNode *last = nullptr;
        while (root || !nodes.empty()) {
            if (root) {
                nodes.push(root);
                current = current*10 + root->val;
                root = root->left;
            } else {
                root = nodes.top();
                if (root->right && root->right != last) {
                    root = root->right;
                } else {
                    nodes.pop();
                    last = root;
                    if (root->left == nullptr && root->right == nullptr)
                        total += current;
                    current /= 10;
                    root = nullptr;
                }
            }
        }
        return total;
    }
};

思路三:

非递归方法,借助栈实现,在对二叉树的遍历过程中,将每次遍历到的节骨所组成的数字存储到该节点的值中,遇到叶子可以直接累加即可,这样操作修改了二叉树的值,相当于修改了二叉树结构,不符合题意,但是如果只是为了找出结果,也可以达到目的。

代码:java版:2ms

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int sumNumbers(TreeNode root) {
        if (root == null) return 0;
        int sum = 0;
        TreeNode curr;
        Stack<TreeNode> ws = new Stack<TreeNode>();
        ws.push(root);
        
        while (!ws.empty()) {
            curr = ws.pop();
            
            if (curr.right != null) {
                curr.right.val = curr.val*10 + curr.right.val;
                ws.push(curr.right);
            }
            if (curr.left != null) {
                curr.left.val = curr.val*10 + curr.left.val;
                ws.push(curr.left);
            }
            if (curr.left == null && curr.right == null) //叶子节点
                sum += curr.val;
        }
        return sum;
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: