【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(全部根到叶子结点组组成的数字相加)】...相关推荐

  1. 【同113】LeetCode 129. Sum Root to Leaf Numbers

    LeetCode 129. Sum Root to Leaf Numbers Solution1:我的答案 二叉树路径和问题,类似113 /*** Definition for a binary tr ...

  2. 129. Sum Root to Leaf Numbers

    输入: [1,2,3]1/ \2 3 输出: 25 解释: 从根到叶子节点路径 代表数字 . 从根到叶子节点路径 代表数字 . 因此,数字总和 = 12 + 13 = 25 class Solutio ...

  3. LeetCode Sum Root to Leaf Numbers (dfs)

    问题:给出一个二叉树,求从根结点到叶子结点路径上构成的所有数的和 思路:自顶向下遍历过程中,递归的终止条件是,如果是叶子结点,返回上一层的数*10加上叶子结点的值.在遍历过程中,如果存在左子树,计算左 ...

  4. 【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 ...

  5. 【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 ...

  6. 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】

    [015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...

  7. 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】...

    [109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 ...

  8. 【LeetCode-面试算法经典-Java实现】【054-Spiral Matrix(螺旋矩阵)】

    [054-Spiral Matrix(螺旋矩阵)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a matrix of m x n elements (m ...

  9. 【LeetCode-面试算法经典-Java实现】【226-Invert Binary Tree(反转二叉树)】

    [226-Invert Binary Tree(反转二叉树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun- ...

最新文章

  1. Java中使用hql,sql查询--多表查询后的新实体构建方法
  2. 前端学习(2925):vue改变样式1
  3. linux 库的头文件安装在,“找不到jpeg的头文件或库文件”在Alpine Linux上安装枕头...
  4. 第十一章 Helm-kubernetes的包管理器(上)
  5. 11.28.cookie
  6. BZOJ1853: [Scoi2010]幸运数字(容斥原理)
  7. PHP 实现文件下载
  8. outlook邮箱邮件大小限制_如何解除OutLook邮件附件大小限制?成功发送大附件的邮件...
  9. android优化最强软件,手机提速谁最行?十款安卓优化软件比拼
  10. 程序员最爱用的在线代码编辑器合集,哪款是你的最爱?
  11. 升级JDK版本注意事项
  12. linux加载模块失败,linux 第二次加载netlink模块时,内核创建sock失败
  13. Dr.com 新版 Ver 5.2.0 本地 密码 读取 查看 破解
  14. 转:System32与SysWOW64的关系
  15. SQL-DQL(数据库查询语言)
  16. java 数字和字母_java-如何在字母和数字之间(或数字和字母之间)分割字符串?...
  17. 183 25用计算机算列竖式,新北师大四年级上册数学计算题
  18. 怎样用计算机求方程组,【教程】用计算器解方程(牛顿法)
  19. H3c网络互联学习笔记——网络设备的基本操作
  20. linux分区方案 500g,linux系统分区建议

热门文章

  1. 简述开发Android2D图形,2D图形开发(一)(高级).pdf
  2. 查询数据库现在几个用户在连接
  3. Python 语言程序设计(2)基本图形绘制
  4. 中南大学 科学计算与MATLAB语言 矩阵的特征值与特征向量
  5. 系统学习深度学习(二十八)--DSD
  6. 表情识别(三)--基于几何与Gabor小波的多层感知
  7. 相机模型与标定(八)--传统相机标定算法简介
  8. 相机模型与标定(六)--单应性求解
  9. HTTP协议到底是怎么回事
  10. Hive 动态分区入门