Sum Root to Leaf Numbers leetcode java
题目:
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
.
题解:
分析这道题,由根节点往叶节点方向走,就是从高位往地位加和的方向。也就是说,当遍历的节点往叶节点方向走一层的时候,该节点的值应为父节点的值*10+当前节点的值。
由此可以写出代码:
1 int sumhelper(TreeNode root, int levelBase) {
2 if(root == null)
3 return 0;
4
5 if(root.left == null && root.right == null) {
6 return levelBase + root.val;
7 }
8
9 int nextLevelBase = (levelBase + root.val)*10 ;
int leftSubTreeSum = sumhelper(root.left, nextLevelBase);
int rightSubTreeSum = sumhelper(root.right, nextLevelBase);
return leftSubTreeSum + rightSubTreeSum;
}
public int sumNumbers(TreeNode root) {
return sumhelper(root,0);
}
Sum Root to Leaf Numbers leetcode java的更多相关文章
- Sum Root to Leaf Numbers——LeetCode
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- Sum Root to Leaf Numbers [LeetCode]
Problem description: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ Basic idea: To store ...
- 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 ...
- Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...
- 【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 & 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 ...
- 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 ...
- Java for 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 ...
随机推荐
- 利用Microsoft Sql Server Management studio 创建数据库的示例
利用Microsoft Sql Server Management studio 创建数据库的示例方法如下: 一.打开安装好的Microsoft Sql Server Management stu ...
- MD Test
欢迎使用 MWeb XXX ``` ZFCollectionViewListController.m <script src="https://blog-static.cnblogs. ...
- [CC-FNCS]Chef and Churu
[CC-FNCS]Chef and Churu 题目大意: 一个长度为\(n(n\le10^5)\)的数列\(A_{1\sim n}\),另有\(n\)个函数,第\(i\)个函数会返回数组中标号在\( ...
- Gitlab使用QQ企业邮箱发送邮件
注册QQ企业邮箱 地址 https://exmail.qq.com/signupfree?refer=intro#signup/free 注册完成后解析 编辑/etc/gitlab/gitlab.rb ...
- Windows和linux下clock函数
windows: Calculates the wall-clock time used by the calling process. return:The elapsed wall-clock ...
- Eclipse中执行Maven命令时控制台输出乱码
Maven 默认编码为 GBK: 在 Eclipse 控制台输出乱码: 解决方法:将以下代码添加到 pom.xml 的 <project> 节点下: <project> …… ...
- nginx+uwsgi+flask 服务器配置
注:每个机器,软件版本可能不一样,虽然网上有很多类似的帖子,但是我在搭建的时候遇到了不少的坑,此文仅供参考. 请求流程: 1.安装uwsgi uwsgi是一个应用服务器,非静态文件的网络请求就必须通过 ...
- logstash grok 分割匹配日志
使用logstash的时候,为了更细致的切割日志,会写一些正则表达式. 使用方法 input { file { type => "billin" path => &qu ...
- nuxus 3在Maven项目的配置和POM文件的配置
在nuxus 3中的Maven默认会创建三个仓库,这三个仓库的关系如下: public是release和snapshot的全集,release默认为关闭状态,所以在配置nexus 3时需要将其开启. ...
- CSS动画简介
现在,我很少写介绍CSS的文章,因为感觉网站开发的关键还是在服务器端. 但是,CSS动画除外,它实在太有用了. 本文介绍CSS动画的两大组成部分:transition和animation.我不打算给出 ...