码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Sum Root to Leaf Numbers"

时间:2014-07-17 09:55:25      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   re   div   

A typical DFS usage, no big deal:

class Solution {
public:    
    int dfs(TreeNode *p, int pval)
    {
        if( !p->left && !p->right )
        {
            int ssum = pval * 10 + p->val;            
            return ssum;
        }

        int rl = 0, rr = 0;
        if(p->left)  rl = dfs(p->left, pval * 10 + p->val);
        if(p->right) rr = dfs(p->right, pval * 10 + p->val);
        return rl + rr;
    }
    int sumNumbers(TreeNode *root) {
        int ret = 0;
        if(root)    ret = dfs(root, 0);
        return ret;
    }
};

LeetCode "Sum Root to Leaf Numbers",布布扣,bubuko.com

LeetCode "Sum Root to Leaf Numbers"

标签:style   blog   color   io   re   div   

原文地址:http://www.cnblogs.com/tonix/p/3850102.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!