树——sum-root-to-leaf-numbers(根到叶节点数字之和)
问题:
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path1->2->3which represents the number123.
Find the total sum of all root-to-leaf numbers.
For example,
1
/ \
2 3
The root-to-leaf path1->2represents the number12.
The root-to-leaf path1->3represents the number13.
Return the sum = 12 + 13 =25.
代码:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int sumNumbers(TreeNode root) {
int sum = 0;
if(root==null)
return sum;
return sumNumbers(root,sum);
} public int sumNumbers(TreeNode root, int sum){
if(root==null)
return 0;
sum = sum*10+root.val;
if(root.left==null && root.right==null)
return sum;
return sumNumbers(root.left, sum) + sumNumbers(root.right, sum);
}
}
树——sum-root-to-leaf-numbers(根到叶节点数字之和)的更多相关文章
- [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 ...
- [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: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语言递归之求根到叶节点数字之和
题目描述 给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点 ...
- Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...
- 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 ...
- 【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 ...
- 23. 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 ...
- 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 ...
随机推荐
- es之Source字段和store字段
PUT /website/blog/ { "title" : "elasticsearchshi是是什么", "author" : &quo ...
- 顶级域名、一级域名、二级域名与IP
转自:https://blog.csdn.net/qq_38071429/article/details/80339091 域名:可分三级,一级域名,二级域名,三级域名.是由一串字符+域名后缀组成,我 ...
- goroutine 分析 协程的调度和执行顺序 并发写
package main import ( "fmt" "runtime" "sync" ) const N = 26 func main( ...
- 十七、jenkins运行robotframework脚本,配置自动发送邮件
一.配置系统管理-系统设置: A.系统管理--系统设置--Jenkins Location-系统管理员邮件地址:配置管理员邮箱全称(qq,163等都可以) B.配置管理员邮箱属性: 1.输入smtp服 ...
- 【C++进阶:STL常见性质】
STL中的常用容器包括:顺序性容器(vector.deque.list).关联容器(map.set).容器适配器(queue.stac) 转载自:https://blog.csdn.net/u0134 ...
- Caffe:深入分析(怎么训练)
main() 首先入口函数caffe.cpp int main(int argc, char** argv) { ...... ) { #ifdef WITH_PYTHON_LAYER try { # ...
- GMSSL中生成SM2或RSA1024或RSA2048的证书相关命令
1.生成KEY:gmssl sm2 -genkey -out 01.root.pemgmssl genrsa -out 01.root_plain.key 2048gmssl genrsa -out ...
- DataGridView数值列和日期列
本文转自:http://www.cnblogs.com/conexpress/p/5923324.html 在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会 ...
- jsp+servlet的简单实现
开发环境 tomcat7.0,MyEclipse 10 1.建一个简单的Web Project ,项目名jspServlet: 2.在src目录下建一个package ,为com.fandy.serv ...
- Docker中使用多阶段Dockerfile构建容器镜像image(镜像优化)
使用多阶段构建 预计阅读时间: 6分钟 多阶段构建是守护程序和客户端上需要Docker 17.05或更高版本的新功能.多阶段构建对于那些努力优化Dockerfiles同时使其易于阅读和维护的人来说非常 ...