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.
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,
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.
分析
按照二叉树的深度遍历,累计所有路径组成整数的和。
使用二叉树的递归深度优先遍历实现!
AC代码
/**
* Definition for a binary tree node.
* 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 ret = 0;
if (!root)
return ret;
else if (!root->left && !root->right)
{
ret = root->val;
return ret;
}
else
dfs(root, root->val , ret);
return ret;
}
//深度优先遍历,得到所有根节点到叶子节点的路径和
void dfs(TreeNode *root, int val, int &sum)
{
if (!root->left && !root->right)
sum += val;
if (root->left)
{
dfs(root->left, val * 10 + root->left->val, sum);
}
if (root->right)
{
dfs(root->right, val * 10 + root->right->val, sum);
}
}
};
LeetCode(129) 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之深度优先搜索(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 解题报告
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 ...
- 【leetcode】Sum Root to Leaf Numbers(hard)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 129. Sum Root to Leaf Numbers(从根节点加到叶子节点的和)
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a numb ...
随机推荐
- (转)Module ngx_http_fastcgi_module
Example ConfigurationDirectives fastcgi_bind fastcgi_buffer_size fastcgi_buffering f ...
- jquery自定义组件开发
jquery的组件已经有很多,但是有可能找不到符合我们需求的组件,所以我们可以动手自己封装一个jquery组件. 第一步要知道封装jquery组件的基本语法 (function ($) { $.fn. ...
- Eclipse启动SDK Manager报错:[SDK Manager] 'xcopy' 不是内部或外部命令,也不是可运行的程序。
解决方法,在path环境变量下加上C:\WINDOWS\system32;或将C:\WINDOWS\system32\xcopy.exe拷贝到android sdk目录的tools下面即可正常运行.
- 洛谷P1781 宇宙总统
https://www.luogu.org/problem/show?pid=1781 高精比较大小: #include<iostream> #include<cstdio> ...
- VS 解决方案文件结构分析
VS2013 解决方案文件结构分析 Visual Studio 的解决方案文件是一个文本文件,其中的内容不是太复杂,有些时候 Visual Studio 会把这个文件搞乱,理解一下这个文件的结构,对我 ...
- Spring中统一相同版本的api请求路径的一些思考
Spring中统一相同版本的api请求路径的一些思考 问题场景 当我们在实际开发中,可能会遇到开发相同同版本的api, 假设相同版本的api请求路径为/v1/functionA,/v1/functio ...
- 【转】HTTPS系列干货(一):HTTPS 原理详解
HTTPS系列干货(一):HTTPS 原理详解 前言 HTTPS(全称:HyperText Transfer Protocol over Secure Socket Layer),其实 HTTPS 并 ...
- 【转】HEIF图片存储格式探秘
HEIF图片存储格式探秘 2017年12月11日 18:30:43 阅读数:891 HEIF,High Efficiency Image File Format,即高效率图档格式,是由动态图像专家组( ...
- angularjs实现导航菜单切换高亮
<ul> <li ng-repeat="(index, item) in headerList"> <a ui-sref="{{item.h ...
- Android用RecyclerView实现的二维Excel效果组件
excelPanel 二维RecyclerView.不仅可以加载历史数据,而且可以加载未来的数据. 包括在您的项目中 excelPanel 二维RecyclerView.不仅可以加载历史数据,而且 ...