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.

从根节点開始,dfs的思路,事实上也就是postorder(先序遍历),遍历路径上的值每次乘以基数10,过程非常easy,代码例如以下:

class Solution {
public:
int sum; void dfs(TreeNode *root, int pre){
if (root == NULL)
return; int current = root->val + 10 * pre;
if (root->left == NULL && root->right == NULL){
sum += current;
return;
} if (root->left)
dfs(root->left, current);
if (root->right)
dfs(root->right, current);
} int sumNumbers(TreeNode *root) {
sum = 0;
dfs(root, 0);
return sum;
}
};

LeetCode :: Sum Root to Leaf Numbers [tree、dfs]的更多相关文章

  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] Sum Root to Leaf Numbers 求根到叶节点数字之和

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

  3. [leetcode]Sum Root to Leaf Numbers @ Python

    原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...

  4. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

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

  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 number ...

  6. LeetCode: Sum Root to Leaf Numbers [129]

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

  7. 129. Sum Root to Leaf Numbers(Tree; DFS)

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

  8. [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

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

  9. LeetCode Sum Root to Leaf Numbers(DFS)

    题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...

随机推荐

  1. JS基础用法-向数组指定位置插入对象

    在做省市区三级联动的时候,需要在省市区默认位置放上请选择字样. 由于后台的API接口返回的没有请选择字样,那么就需要给返回的数组手动增加请选择 代码如下 // 原来的数组 var array = [& ...

  2. 在windows 10 64bit系统上安装python 3.6 64bit的numpy模块

    1.查找自己的python版本对照的whl文件(cp36代表的是版本) 地址:https://pypi.python.org/pypi/numpy 2.下载完毕执行一下命令即可 pip install ...

  3. (25)C#windows服务

    http://www.cnblogs.com/knowledgesea/p/3616127.html http://jingyan.baidu.com/article/fa4125acb71a8628 ...

  4. Codeforces 935E Fafa and Ancient Mathematics(表达式转树 + 树型DP)

    题目链接  Codeforces Round #465 (Div. 2) Problem E 题意  给定一个表达式,然后用$P$个加号和$M$个减号填充所有的问号(保证问号个数等于$P + M$) ...

  5. Python的并发并行[1] -> 线程[0] -> threading 模块

    threading模块 / threading Module 1 常量 / Constants Pass 2 函数 / Function 2.1 setprofile()函数 函数调用: thread ...

  6. 【贪心】Mixing Milk

    题目描述 The Merry Milk Makers company buys milk from farmers, packages it into attractive 1- and 2-Unit ...

  7. android中的开机自启动

    android中的开机自启动 android中的开机自启动可分为两步: 1.写一个BroadcastReceiver: public class BootReceiver extends Broadc ...

  8. Android Developer -- Bluetooth篇 开发实例之三 管理连接

    Managing a Connection When you have successfully connected two (or more) devices, each one will have ...

  9. Tiny6410下的第一个Linux驱动程序

    Linux系统环境是照着友善之臂的教程搭建的 //Hello  World驱动程序源文件 #include <linux/miscdevice.h> #include <linux/ ...

  10. linux命令详解:cat命令

    转:http://www.cnblogs.com/lwgdream/archive/2013/11/06/3409802.html 前言 cat命令用于读取文本文件,并且能够显示行号.特殊字符等. 使 ...