【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】...
【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】
转载于:https://www.cnblogs.com/claireyuancy/p/7029019.html
【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】...相关推荐
- 【同113】LeetCode 129. Sum Root to Leaf Numbers
LeetCode 129. Sum Root to Leaf Numbers Solution1:我的答案 二叉树路径和问题,类似113 /*** Definition for a binary tr ...
- 129. Sum Root to Leaf Numbers
输入: [1,2,3]1/ \2 3 输出: 25 解释: 从根到叶子节点路径 代表数字 . 从根到叶子节点路径 代表数字 . 因此,数字总和 = 12 + 13 = 25 class Solutio ...
- LeetCode Sum Root to Leaf Numbers (dfs)
问题:给出一个二叉树,求从根结点到叶子结点路径上构成的所有数的和 思路:自顶向下遍历过程中,递归的终止条件是,如果是叶子结点,返回上一层的数*10加上叶子结点的值.在遍历过程中,如果存在左子树,计算左 ...
- 【LeetCode从零单排】No129 Sum Root to Leaf Numbers
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
- 【LeetCode】Sum Root to Leaf Numbers
题目 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a num ...
- 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】
[015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...
- 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】...
[109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 ...
- 【LeetCode-面试算法经典-Java实现】【054-Spiral Matrix(螺旋矩阵)】
[054-Spiral Matrix(螺旋矩阵)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a matrix of m x n elements (m ...
- 【LeetCode-面试算法经典-Java实现】【226-Invert Binary Tree(反转二叉树)】
[226-Invert Binary Tree(反转二叉树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun- ...
最新文章
- Java中使用hql,sql查询--多表查询后的新实体构建方法
- 前端学习(2925):vue改变样式1
- linux 库的头文件安装在,“找不到jpeg的头文件或库文件”在Alpine Linux上安装枕头...
- 第十一章 Helm-kubernetes的包管理器(上)
- 11.28.cookie
- BZOJ1853: [Scoi2010]幸运数字(容斥原理)
- PHP 实现文件下载
- outlook邮箱邮件大小限制_如何解除OutLook邮件附件大小限制?成功发送大附件的邮件...
- android优化最强软件,手机提速谁最行?十款安卓优化软件比拼
- 程序员最爱用的在线代码编辑器合集,哪款是你的最爱?
- 升级JDK版本注意事项
- linux加载模块失败,linux 第二次加载netlink模块时,内核创建sock失败
- Dr.com 新版 Ver 5.2.0 本地 密码 读取 查看 破解
- 转:System32与SysWOW64的关系
- SQL-DQL(数据库查询语言)
- java 数字和字母_java-如何在字母和数字之间(或数字和字母之间)分割字符串?...
- 183 25用计算机算列竖式,新北师大四年级上册数学计算题
- 怎样用计算机求方程组,【教程】用计算器解方程(牛顿法)
- H3c网络互联学习笔记——网络设备的基本操作
- linux分区方案 500g,linux系统分区建议