【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.
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^x+子节点到页节点生成的整数的时候不能确定x的值,因为不知道父节点到叶节点的距离。所以这种方法不可行。
后来想了下,有个更简单的方法:将最终生成的整数存放在叶节点而不是根节点。每次递归的时候,在当前节点计算一个sum,表示从根节点到当前节点生成的整数,然后把这个sum传递给当前节点的孩子,在孩子上递归的计算从根节点到孩子节点生成的整数.....
举个例子,如下图所示的一棵树:
最后将各个节点上的sum相加(123+125+146)就得到了最终的答案。
代码如下:
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int totalSum = 0;
private void rootList(TreeNode root,int sum){
if(root == null)
return;
sum = sum*10+root.val; if(root.left == null && root.right == null)
{
totalSum += sum;
return;
} rootList(root.left, sum);
rootList(root.right, sum); }
public int sumNumbers(TreeNode root) {
rootList(root, 0);
return totalSum;
}
}
【leetcode刷题笔记】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 represe ...
- 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 num ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 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之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)
Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...
- 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】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刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
随机推荐
- XSD文件详解(二)
<?xml version="1.0" encoding="gb2312"?> <studentlist> <student ...
- ios __block typeof 编译错误解决
type specifier missing a parameter list without types is only allowed in a function definition 解决: 工 ...
- openCV图像形态学
#include <cv.h> #include <highgui.h> #include <stdio.h> //平滑处理 int main() { IplIma ...
- What happens when we continue stacking deeper layers on a “plain” convolutional neural network?
http://cs231n.stanford.edu/slides/2017/cs231n_2017_lecture9.pdf The deeper model performs worse, but ...
- 【python】-- paramiko、跳板机(堡垒机)
paramiko Python的paramiko模块,该模块用于连接远程服务器并执行相关命令,常用于作批量管理使用 一.下载: pip3 install paramiko 源码:查看 二.parami ...
- 【thrift】初识thrift
Reference:http://www.ibm.com/developerworks/cn/java/j-lo-apachethrift/ http://jacksongblack.blog.51c ...
- 阿里云 rails nginx 配置https访问
1.申请免费型dv ssl证书:https://common-buy.aliyun.com/?spm=a2c4e.11155515.0.0.7zzvOZ&commodityCode=cas#/ ...
- PAT天梯赛 L2-019. 悄悄关注 【STL】
题目链接 https://www.patest.cn/contests/gplt/L2-019 思路 将已关注的人 用 MAP存起来 然后将点赞的用户中 没有关注的 用 VECTOR 存下来 并且求出 ...
- iOS swift 常量 && 宏定义
全局常量 在C和Objective-C语言源文件中定义的全局常量会自动地被Swift编译引进并做为Swift的全局常量. 预处理指令 Swift编译器不包含预处理器.取而代之的是,它充分利用了编译时属 ...
- UIImageView 获取图片的 宽 高
该文章纯属这两天开发的经验之谈 并且也是平常没注意 这回发现的一个小方法 并且很实用 在开发中 提高了很大的效率 更加符合高保真的要求 通常 美术 切的一些图片 需要 :1还原的 现在 我们一般支持i ...