LeetCode :: Sum Root to Leaf Numbers [tree、dfs]

时间:2021-07-05 00:52:15

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. 导航(NavanavigationController)push和pop

    //跳转到指定的控制器 for (UIViewController *Vc in self.navigationController.viewControllers) { if ([Vc isKind ...

  2. [swustoj 411] 售货员的难题

    售货员的难题(0411) Time limit(ms): 5000 Memory limit(kb): 65535 Submission: 1744 Accepted: 200 Description ...

  3. python-凯撒密码

    凯撒密码 简介:凯撒密码(Caesar)是最早的代换密码,对称密码的一种 算法:将每个字母用字母表中它之后的第k(称作位移值)个字母替代 代码: #-*-coding:utf-8-*- __autho ...

  4. hdu4190 简单的二分法

    题意是 有n个城市,m个投票箱.接下来n个城市人口数,每一个投票箱都不能为空.计算最后投票箱的容量必须达到多少,才干满足须要. 每一个城市的人必须仅仅能将票投到自己城市分得得投票箱中.要是容量最小箱子 ...

  5. DOM操作-根据name获取网页中的全部复选框

    描述: 与id不同,多个元素可以使用相同的name属性,如果需要获取这一类元素的DOM对象,就需要使用getElementsByName()函数 代码: <!DOCTYPE html> & ...

  6. vue视频学习笔记02

    video 2 vue制作weibo交互 vue-> 1.0vue-resource ajax php服务器环境(node) this.$http.get()/post()/jsonp() th ...

  7. centos7安装nodejs

    方法一.https://github.com/nodesource/distributions#rpminstall 按照上面地址中的教程安装完后,使用node -v命令报错: -bash: /usr ...

  8. win2003服务器定时自动重启命令

    1. win2003可以这样自动重启: 新建一个命令行文件比如reboot.bat 内容如下:shutdown -r -t 30 在计划任务中新建一个任务,程序选择上面这个reboot.cmd文件,时 ...

  9. Centos 7&period;0 execute yum update ——File &quot&semi;&sol;usr&sol;libexec&sol;urlgrabber-ext-down&quot&semi;&comma; line 75&comma; in &lt&semi;module&gt&semi; 解决方式

    [打开这个文件:/usr/lib/python2.7/site-packages/urlgrabber/grabber.py找到elif errcode in (42, 55,56)   用  eli ...

  10. Java学习笔记10(面向对象三:接口)

    接口: 暂时可以理解为是一种特殊的抽象类 接口是功能的集合,可以看作是一种数据类型,是比抽象类更抽象的"类" 接口只描述所应该具备的方法,并没有具体实现,具体实现由接口的实现类(相 ...