[LeetCode] 129. Sum Root to Leaf Numbers

45 篇文章 0 订阅

原题链接:https://leetcode.com/problems/sum-root-to-leaf-numbers/

1. 题目介绍

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.
Note: A leaf is a node with no children.

给出一个二叉树,每个节点有一个0-9的个位数,每一个从根节点到叶子节点的路径都可以代表一个数字。
例如,路径1->2->3代表了数字123
寻找所有从根节点开始,在叶子节点结束的路径所代表的数字之和。
叶子节点就是指没有子节点的节点。

Example 1:
在这里插入图片描述
Explanation:
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Therefore, sum = 12 + 13 = 25.

Example 2:
在这里插入图片描述
Explanation:
The root-to-leaf path 4->9->5 represents the number 495.
The root-to-leaf path 4->9->1 represents the number 491.
The root-to-leaf path 4->0 represents the number 40.
Therefore, sum = 495 + 491 + 40 = 1026.

2. 解题思路

可以使用深度优先搜索的顺序,找到所有从根节点出发,到叶子节点结束的路径。在寻找这些路径的同时,使用一个 List 记录经过节点的 val 值。当到达叶子节点时,将List中的所有值按照顺序取出,并且转换成 int 整数,将这个整数加到总和 sum 里面去。最终的 sum 就是我们要寻找的总和了。

实现代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    List<Integer> l;
    int sum = 0;
    public int sumNumbers(TreeNode root) {
        if(root == null){
            return 0;
        }
        l = new ArrayList<>();
        dfs(root);
        return sum;
    }
    public void dfs(TreeNode node){
        if(node == null){
            return;
        }
        l.add(node.val);
        if(node.left == null && node.right == null){
            sum += listToInt(l);
        }
        dfs(node.left);
        dfs(node.right);
        l.remove(l.size()-1);
    }
    public int listToInt(List<Integer> list){
        int ans = 0;
        for(int i: list){
            ans = ans*10 + i;
        }
        return ans;
    }
}

使用同样的思路,还可以对代码进行精简。
LeetCode 中速度最快的方法根本没有使用List,而是使用函数的参数传递来保留当前计算的总和的。
可以让传入的参数多一个数total,这个数代表了走到当前位置时,已经求得的总和是多少。
同时还需要返回 int 类型的数,代表着到达改节点时,左右子树的总和是多少。

实现代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int sumNumbers(TreeNode root) {
        if(root == null){
            return 0;
        }
        return sum(root , 0);
    }
    public int sum(TreeNode node, int total){
        if(node == null){
            return 0;
        }
        total += node.val;
        if(node.left == null && node.right == null){
            return total;
        }
        return sum(node.left, total * 10) + sum(node.right , total*10);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值