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.
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
.
class Solution {
public:
int sumNumbers(TreeNode *root) {
int sum = ;
if(root)
dfs(root, , sum);
return sum; }
void dfs(TreeNode *node, int val, int& sum) {
val *= ;
val += node->val; if (node->left == NULL && node->right == NULL) {
sum += val;
return;
} if (node->left) {
dfs(node->left, val, sum);
}
if (node->right) {
dfs(node->right, val, sum);
}
}
};
129. Sum Root to Leaf Numbers(Tree; DFS)的更多相关文章
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【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 ...
- LeetCode :: 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 ...
- leetcode@ [129] Sum Root to Leaf Numbers (DFS)
https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...
- 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 ...
- [LeetCode] 129. Sum Root to Leaf Numbers_Medium tag: DFS
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- [LeetCode] 129. Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 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 ...
- 【LeetCode】129. Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
随机推荐
- C# 线程间不能调用剪切板的问题
最近做一个项目,需要用到线程,而且要用到剪切板,创建了一个子线程之后发现在子线程中剪切板上获取不到数据,当时特别纳闷,上网查资料,最后终于搞定,现将解决方法归纳如下: 第一步: public void ...
- open和close函数
1.open函数的使用 调用open函数可以打开或创建一个文件 #include <sys/stat.h> #include <fcntl.h> #include <sy ...
- Centos6.8 安装MySql
启动Centos6.8 输入命令: yum install mysql mysql-server -y 等待安装完成. 启动MySQL,输入命令: /etc/init.d/mysqld s ...
- Oracle导出导入表空间创建
//备份数据库前的sqlplus命令创建数据库dmp存入目录 sqlplus /nolog conn /as sysdba SQL> create or replace directory ex ...
- JSONUtils的几个常用方法
1.首先新建1个JSONUtils类 public class JSONUtils { /** * * @author wangwei JSON工具类 * @param * */ /*** * 将Li ...
- winform 控件随页面大小进行自适应
这个功能网上很多人在问,也有不少人给出过答案,经过实际使用,觉得网上这段代码实现的效果比较好,记录一下 核心代码就是下面这个类 using System; using System.Collectio ...
- ContentProvider用法
1.通过Context的getContentRsolver()获取ContentResolver类的实例. 2.ContentResolver中接收的不是表明而是内容URI 3.解析内容URI获得Ur ...
- HBase之五:hbase的region分区
一.Region 概念 Region是表获取和分布的基本元素,由每个列族的一个Store组成.对象层级图如下: Table (HBase table) Region (Regions for the ...
- python学习笔记(七):面向对象编程、类
一.面向对象编程 面向对象--Object Oriented Programming,简称oop,是一种程序设计思想.在说面向对象之前,先说一下什么是编程范式,编程范式你按照什么方式来去编程,去实现一 ...
- preprocess
1,宏定义,有参宏,无参宏,宏定义实现的是定义一个符号常量; 条件编译3种方式,文件包含含义; 不带参数的宏定义;既用一个指定的的标识符来代替一个字符串; #define RUIY 10000000 ...