【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】


【LeetCode-面试算法经典-Java实现】【全部题目文件夹索引】

原题

  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。根到叶子组成一个数字,求全部的根到叶子组成的数字的和。

解题思路

  採用回溯法。

代码实现

算法实现类

public class Solution {

    private int result = 0; // 记录总的结果
private int num = 0; // 记根到叶子的数字 public int sumNumbers(TreeNode root) {
sum(root);
return result;
} private void sum(TreeNode root) {
if (root != null) {
num = num*10 + root.val; // 已经到了根结点了
if (root.left == null && root.right == null) {
result += num;
}
sum(root.left);
sum(root.right);
num /= 10;
}
}
}

评測结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗体中查看完整图片。

特别说明

欢迎转载。转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47678205

【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】的更多相关文章

  1. [LeetCode] 129. Sum Root to Leaf Numbers 求根到叶节点数字之和

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  2. 129 Sum Root to Leaf Numbers 求根叶数字总和

    给定一个只包含 0-9 数字的二叉树,每个根到叶的路径可以代表一个数字.例如,从根到叶路径 1->2->3则代表数字 123.查找所有根到叶数字的总和.例如,    1   / \  2  ...

  3. Leetcode129. Sum Root to Leaf Numbers求根到叶子节点数字之和

    给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点生成的所有 ...

  4. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  5. 【LeetCode】129. Sum Root to Leaf Numbers (2 solutions)

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  6. Java for LeetCode 129 Sum Root to Leaf Numbers

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  7. leetcode 129. Sum Root to Leaf Numbers ----- java

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  8. leetcode@ [129] Sum Root to Leaf Numbers (DFS)

    https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...

  9. [LeetCode] 129. Sum Root to Leaf Numbers 解题思路

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

随机推荐

  1. 洛谷——P3258 [JLOI2014]松鼠的新家

    https://www.luogu.org/problem/show?pid=3258 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到 ...

  2. ZOJ 2836

    求不比M大的可以被集合任一个数整除的数的个数.(容斥原理) #include <iostream> #include <cstdio> #include <algorit ...

  3. Android开发之AlarmManager具体解释

    AlarmManager实质是一个全局的定时器,是Android中经常使用的一种系统级别的提示服务,在指定时间或周期性启动其他组件(包含Activity,Service,BroadcastReceiv ...

  4. webserver的作用!

    常见的webserver:Apache和Nginx(linux平台) IIS(window平台) 非常多小公司小项目眼下不一定使用了webserver,由于流量下,单台应用server能够支撑. 实际 ...

  5. Win7上从硬盘安装Debian

    近期一直想将笔记本搞成Win7+Debian双系统.由于无论怎样优化,2G内存的Win7笔记本上开个Linux虚拟机都实在吃力. 经过一段时间的资料搜索.并阅读Debian官方的安装文档,今天最终实现 ...

  6. 去除iframe滚动条

    主页面的IFRAME中添加:scrolling="yes" 子页面程序代码: 让竖条消失: <body style='overflow:scroll;overflow-x:a ...

  7. MFC补码原码反码转换工具

    /*_TCHAR str[100] = { 0 }; wsprintf(str, _T("%d"),num);*/ ; CString str; m_edit1.GetWindow ...

  8. ES6 | 关于class类 继承总结

    子类必须在constructor方法中调用super方法,否则新建实例时会报错.这是因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工.如果不调用super方法,子类就得不到 ...

  9. http请求常出现的状态码

    服务器返回的 响应报文 中第一行为状态行,包含了状态码以及原因短语,用来告知客户端请求的结果. 状态码 类别 原因短语 1XX Informational(信息性状态码) 接收的请求正在处理 2XX ...

  10. 继续ubuntu和遇到的easybcd的坑

    找了很多教程,反复斟酌https://www.zhihu.com/question/34611974 个人感觉前方还有大坑无数.unbuntu四个分区系列——65%根目录,25%home目录,交换分区 ...