[Leetcode] 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.
思路:每条root到叶节点的路径代表着一个数。沿着路径,各节点的值在数中由高位到低位,这个有点像在数组中,数组A[]的元素是0~9的整数,求由整个数组元素构成整数,方法是定义变量sum=0,然后sum=sum*10+A[i]。对应一条路径去看,也可以同样去做,只不过这里当前元素的值为当前节点所对应的值。对整棵二叉树,是左子树所有路径构成整数的和加上右子树。考虑到用递归(DFS)来解,遇到两种情况,一、如何处理节点:空节点、叶节点、一般节点;二、如何求和。
针对问题一,遇到空节点,说明其没有左右孩子了,而且对应的值为0;遇到一般节点,则由sum直接加上当前节点的值;遇到叶节点,说明递归到最底端了,本次的递归结束,返回sum,即递归条件构造好了;针对问题二,类似数组的问题,对整棵二叉树节点值得和
可以写为sum(左子树)+sum(右子树),则递归公式确定了。对于递归算法,我也是一知半解,打算通过做题来熟悉,欢迎大神支招!
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumNumbers(TreeNode *root)
{
int sum=;
return sumSubtree(root,sum);
} int sumSubtree(TreeNode *root,int sum)
{
if(root==NULL) return ; //不存在,则认为值为0
sum=sum*+root->val; if(root->left==NULL&&root->right==NULL) //到达叶节点
return sum; return sumSubtree(root->left,sum)+sumSubtree(root->right,sum);
}
};
[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 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 ...
- Leetcode129. Sum Root to Leaf Numbers求根到叶子节点数字之和
给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点生成的所有 ...
- 129 Sum Root to Leaf Numbers 求根叶数字总和
给定一个只包含 0-9 数字的二叉树,每个根到叶的路径可以代表一个数字.例如,从根到叶路径 1->2->3则代表数字 123.查找所有根到叶数字的总和.例如, 1 / \ 2 ...
- 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]Sum Root to Leaf Numbers @ Python
原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...
- LeetCode Sum Root to Leaf Numbers(DFS)
题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...
- 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 ...
随机推荐
- 如何在Mac OS系统下搭建Java开发环境 配置Java环境变量
1. 打开终端 在finder里面搜索(这台MAC)查找终端 2. 在cdlouiedeAir:~ cdlouie$(cdlouie是我的电脑用户名) 后面输入sudo vim /etc/pr ...
- InnoDB锁冲突案例演示(续)
Preface I've demontstrated several InnoDB locking cases in my previous blog.I'm gonna do the ...
- .net 使用com组件操作word遇到的一些问题
1.警告: 方法“Microsoft.Office.Interop.Word._Document.Close(ref object, ref object, ref object)”和非方法“Micr ...
- <cassert>
文件名: <cassert> (assert.h) 这是一个C语言的诊断库,assert.h文件中定义了一个可作为标准调试工具的宏函数: assert ; 下面介绍这个宏函数:asser ...
- 深度学习笔记 (一) 卷积神经网络基础 (Foundation of Convolutional Neural Networks)
一.卷积 卷积神经网络(Convolutional Neural Networks)是一种在空间上共享参数的神经网络.使用数层卷积,而不是数层的矩阵相乘.在图像的处理过程中,每一张图片都可以看成一张“ ...
- 基于Kubernetes(k8s)的RabbitMQ 集群
目前,有很多种基于Kubernetes搭建RabbitMQ集群的解决方案.今天笔者今天将要讨论我们在Fuel CCP项目当中所采用的方式.这种方式加以转变也适用于搭建RabbitMQ集群的一般方法.所 ...
- 将HTML页面页脚固定在页面底部(多种方法实现)
当一个HTML页面中含有较少的内容时,Web页面的footer部分随着飘上来,处在页面的半腰中间,给视觉效果带来极大的影响,接下来为大家介绍下如何将页脚固定在页面底部,感兴趣的朋友可以了解下 作为一个 ...
- str和repr
在Python2.6和Python3.0以及更早的版本中,在交互式模式下的输出本质上是使用repr,因此对于一些浮点数运算,会显示很多位: 4 / 5.0 #0.8000000000000004 但是 ...
- html+css基础 - 个人备忘录
//======================html部分===================// 表现内容<meta http-equiv="Content-Type" ...
- 软工实践Alpha冲刺(1/10)
队名:我头发呢队 组长博客 作业博客 张杰(组长) 过去两天完成了哪些任务 查阅Python爬取音源的资料,如 Python3爬虫抓取网易云音乐热评实战 Python爬取高品质QQ音乐(2) 如何爬网 ...