首页 > 其他 > 详细

[LeetCode]129. Sum Root to Leaf Numbers路径数字求和

时间:2018-01-26 10:06:29      阅读:194      评论:0      收藏:0      [点我收藏+]

DFS的标准形式

用一个String记录路径,最后判断到叶子时加到结果上。

int res = 0;
    public int sumNumbers(TreeNode root) {
        if (root==null)
            return res;
        helper(root,"");
        return res;
    }
    public void helper(TreeNode root,String s)
    {
        s += root.val+"";
        if (root.left==null&&root.right==null)
        {
            res+=Integer.parseInt(s);
            return;
        }
        if (root.left!=null) helper(root.left,s);
        if (root.right!=null) helper(root.right,s);
    }

 

[LeetCode]129. Sum Root to Leaf Numbers路径数字求和

原文:https://www.cnblogs.com/stAr-1/p/8358036.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!