leetcode--Sum Root to Leaf Numbers
来源:互联网 发布:翻译软件哪个好 编辑:程序博客网 时间:2024/05/29 08:27
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 number123
.
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
.
分类:二叉树
题意:二叉树每条路径上的数组合成一个数,求这些数相加的总和
解法1:非递归后续遍历,每次遍历到叶子节点,计算当前栈内,所有节点(也就是这条路径)所代表的数,加上总和。
解法2:深度搜索。关键在于,对于每个节点,我们要知道它之前和是多少。
原文链接http://blog.csdn.net/crazy__chen/article/details/46523567
阅读全文
0 0
- leetcode 86: Sum Root to Leaf Numbers
- [LeetCode] Sum root to leaf numbers
- LeetCode : Sum Root to Leaf Numbers
- [leetcode] Sum Root to Leaf Numbers
- 【leetcode】Sum Root to Leaf Numbers
- LeetCode之sum root to leaf numbers
- leetcode:Sum Root to Leaf Numbers
- [LeetCode]Sum Root to Leaf Numbers
- [Leetcode]Sum Root to Leaf Numbers
- [leetcode]Sum Root to Leaf Numbers
- leetcode Sum Root to Leaf Numbers
- LeetCode-Sum Root to Leaf Numbers
- [leetcode] Sum Root to Leaf Numbers
- LeetCode 129: Sum Root to Leaf Numbers
- LeetCode - Sum Root to Leaf Numbers
- leetcode之Sum Root to Leaf Numbers
- LeetCode | Sum Root to Leaf Numbers
- LeetCode - Sum Root to Leaf Numbers
- 在CentOS上安装Java环境
- windows 结构化异常
- leetcode--Word Ladder
- css3部分学习笔记
- 把一个矩形变成圆角矩形
- leetcode--Sum Root to Leaf Numbers
- 课后习题:简单文件的相加
- alertover推送api的java httpclient实现实例
- PHP 安装
- Windows上优秀的命令行(Terminal Emulator)工具推荐
- leetcode--Surrounded Regions
- Yii2.0前后台登录分离
- leetcode--Palindrome Partitioning
- thinkphp ajax 跨域请求 Access-Control-Allow-Origin 完美解决