Sum Root to Leaf Numbers

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.

思想: 分三种情况: 1. 当前结点为空,返回0.  2. 叶子结点, 返回当前值.  3. 父结点,返回左右两个 path 值的和。

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
int dfs(TreeNode *root, int sum) {
if(root == 0) return 0;
sum = sum * 10 + root->val;
if(root->left || root->right)
return dfs(root->left, sum) + dfs(root->right, sum);
return sum;
}
class Solution {
public:
int sumNumbers(TreeNode *root) {
return dfs(root, 0);
}
};

23. Sum Root to Leaf Numbers的更多相关文章

  1. LeetCode: Sum Root to Leaf Numbers 解题报告

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

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

  3. LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II

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

  4. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

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

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

  6. [LeetCode] 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. [Swift]LeetCode129. 求根到叶子节点数字之和 | Sum Root to Leaf Numbers

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

  8. LeetCode OJ 129. Sum Root to Leaf Numbers

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

  9. 129. Sum Root to Leaf Numbers pathsum路径求和

    [抄题]: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a ...

随机推荐

  1. DES跨(C# Android IOS)三个平台通用的加解密方法

          #region   跨平台加解密(c# 安卓 IOS)       //  public static string sKey = "12345678";       ...

  2. Java中文档制作与继承

    1:如何制作帮助文档(了解) (1)写一个类 (2)加入文档注释 (3)通过javadoc工具生成即可 javadoc -d 目录 -author -version ArrayTool.java 2: ...

  3. IIS 7.5 高并发参数配置

    IIS 7.5 高并发参数配置 由于之前使用的是默认配置,服务器最多只能处理5000个同时请求,对于高并发请求,参照文档设置10万并发 1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为 ...

  4. git 添加ssh的方法 push免登陆

    在github.com上 建立了一个小项目,可是在每次push  的时候,都要输入用户名和密码,很是麻烦 原因是使用了https方式 push 在termail里边 输入  git remote -v ...

  5. ueditor在使用requirejs时,报ZeroClipboard undefined错误

    再网上找到了 http://blog.csdn.net/xundh/article/details/44536665       这样一篇文章, 其中原因说的很明白了 是因为在有requirejs时, ...

  6. Android使用echarts框架的K线图

    百度echarts框架还是比较强大的,之前有尝试使用它,但毕竟主要使用于web网页端,效果不是很好,所以最终还是取消使用echarts 但之前在使用的过程中遇到些问题,虽然解决很简单,但也花了我不少时 ...

  7. 在yii中使用多个数据库

    背景: 对于一个大公司拥有多个分公司的应用场景下,我们通常需要配置多个sub-database(子数据库)来存储不同的数据纪录. 配置步骤: 1.在application骨架里面的主配置文件main. ...

  8. LeetCode【169. Majority Element】

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  9. gridview的行选择的一个问题

    我想实现这样一个效果:单击gridview的行内任意地方都可以选择该行(就是行内复选框被选中),同时修改该行的背景色.当再次单击行内任意地方又可以取消选择.另外,当单击选择行内复选框时,我希望可以选择 ...

  10. ubuntu12.04+proftpd1.3.4a的系统用户+虚拟用户权限应用实践

    目录: 一.什么是Proftpd? 二.Proftpd的官方网站在哪里? 三.在哪里下载? 四.如何安装? 1)系统用户的配置+权限控制 2)虚拟用户的配置+权限控制   一.什么是Proftpd? ...