LeetCode——sum-root-to-leaf-numbers
Question
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.
Solution
递归遍历,到叶子节点就累加起来,然后需要用一个变量来记录路径中的值。
Code
class Solution {
public:
int sumNumbers(TreeNode *root) {
int total = 0;
string path;
sumNumbersCore(root, path, total);
return total;
}
void sumNumbersCore(TreeNode* root, string path, int& total) {
if (root != NULL) {
// int 转 char
path.push_back('0' + root->val);
if (root->left != NULL)
sumNumbersCore(root->left, path, total);
if (root->right != NULL)
sumNumbersCore(root->right, path, total);
if (root->left == NULL && root->right == NULL) {
total += stoi(path);
path.pop_back();
}
}
}
};
LeetCode——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] 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]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
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- 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 ...
- [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. ...
- LeetCode :: Sum Root to Leaf Numbers [tree、dfs]
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- LeetCode Sum Root to Leaf Numbers(DFS)
题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...
- leetcode Sum Root to Leaf Numbers(所有路径之和)
转载请注明来自souldak,微博:@evagle 观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的.那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最 ...
随机推荐
- Unity3d NGUI UICheckbox
单选按钮: 一,常用属性: 1,CheckSprite:选中后,才显示的Sprite,即为“选中”状态; 2,Starts Checked:true,一开始就显示“选中”状态; 3,RadioButt ...
- 人物FSM
人物有限状态机 之前看这个状态机没看懂,今天又翻出来,看的略懂 FSM在游戏中应用的地方还是挺多的 怪物AI,玩家行为管理 条件(包含若干事件) 条件(包含若干事件) 状态1<--------- ...
- python学习【第八篇】python模块
模块与包 模块的概念 在python中一个.py文件就是一个模块. 使用模块可以提高代码的可维护性. 模块分为三种: python标准库 第三方模块 自定义模块 模块的导入方法 1.import语句 ...
- 【BZOJ4066】简单题 KDtree
[BZOJ4066]简单题 Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y& ...
- 1624 取余最长路(set)
1624 取余最长路 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 佳佳有一个n*m的带权矩阵,她想从(1,1)出发走到(n,m)且只能往右往下移动,她能得到的娱 ...
- jQuery 文档操作方法(append)
这些方法对于 XML 文档和 HTML 文档均是适用的,除了:html(). 一.append() 方法 append() 方法在被选元素的结尾(仍然在内部)插入指定内容. 例子: <html& ...
- JmsTemplate 发送方式
---恢复内容开始--- 背景: 原来我准备是setDefaultDestinationName 设置队列的名称 发现 系统运行后 创建 的并不是队列 ,而是Topic , 自己看下源码,发现在创 ...
- django restframework 的日常使用
本文讨论 django restframework 的日常使用,满足常用 api 编写的需求,比如 List, Detail, Update, Put, Patch 等等.探讨 django rest ...
- 【python】-- Django 中间件、缓存、信号
Django 中间件.缓存.信号 一. Django 中间件 django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的 ...
- getTrim(strMz)-我的JavaScript函数库-mazey.js
因为一些旧版本的浏览器对于JS的trim()不支持,所以可以用正则代替此功能. 参数:strMz,字符串,必需. function getTrim(strMz) { return strMz.r ...