sum-root-to-leaf-numbers leetcode C++
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.
C++
/**
* 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){
if(NULL == root) return 0;
int sum = 0;
sum = getSum(root,0);
//getSum2(root,0,sum);
return sum;
}
void getSum2(TreeNode *root, int num, int &sum){
num = num * 10 + root->val;
if(NULL == root->left && NULL == root->right){
sum += num;
return;
}
if(root->left)
getSum2(root->left, num, sum);
if(root->right)
getSum2(root->right, num, sum);
}
int getSum(TreeNode* root, int num){
num = num * 10 + root->val;
if(NULL == root->left && NULL == root->right)
return num;
int sum = 0;
if (NULL != root->left) sum += getSum(root->left,num);
if (NULL != root->right) sum += getSum(root->right,num);
return sum;
}
};
sum-root-to-leaf-numbers leetcode C++的更多相关文章
- Sum Root to Leaf Numbers——LeetCode
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- Sum Root to Leaf Numbers leetcode java
题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...
- Sum Root to Leaf Numbers [LeetCode]
Problem description: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ Basic idea: To store ...
- 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: 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 ...
- 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 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
随机推荐
- python 爬虫新手入门教程
python 爬虫新手教程 一.什么是爬虫 爬虫就是把一个网站里的内容读取下来 这里我们就要学习一个知识 我们看到的网页是有一种叫HTML的语言编写的 他可以给文字显示不同的样式 如:<p> ...
- PHP中的输出缓冲控制
在 PHP 中,我们直接进行 echo . 或者 print_r 的时候,输出的内容就会直接打印出来.但是,在某些情况下,我们并不想直接打印,这个时候就可以使用输出缓冲控制来进行输出打印的控制.当然, ...
- css3 flex的IE8浏览器兼容问题
我这是进行判断浏览器 css判断ie版本才引用样式或css文件 <!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--> &l ...
- 从浏览器渲染层面解析css3动效优化原理
引言 在h5开发中,我们经常会需要实现一些动效来让页面视觉效果更好,谈及动效便不可避免地会想到动效性能优化这个话题: 减少页面DOM操作,可以使用CSS实现的动效不多出一行js代码 使用绝对定位脱离让 ...
- 『Python』matplotlib的imshow用法
热力图是一种数据的图形化表示,具体而言,就是将二维数组中的元素用颜色表示.热力图之所以非常有用,是因为它能够从整体视角上展示数据,更确切的说是数值型数据. 使用imshow()函数可以非常容易地制作热 ...
- virtualbox nat 模式下连接虚拟机redis
主要是使用端口转发的方法 如果你能使用xshell等工具连接这个方法基本一样 接着修改redis.conf文件的69 行(我使用的是5.0)将这里的地址修改为虚拟机的 ip 地址,这里我使用的是备份 ...
- 图数据库Neo4j的基本使用及与SpringBoot集成
Neo4j 官网地址:https://neo4j.com/ 下载地址:https://neo4j.com/download-center/#community 官方入门文档:https://neo4j ...
- firewalld dbus接口使用指南
firewalld,一个基于动态区的iptables/nftables守护程序,自2009年左右开始开发,最新版本 - 防火墙0.6.3 - 发布于2018年10月11日.主要的开发人员是托马斯·沃纳 ...
- 宝塔Linux面板搭建与安全狗安装(WEB服务器搭建与WAF安装)
环境 系统:CentOS 7.3 (64位) 软件: 宝塔Linux 7.7 网站安全狗Linux-Apache版V2.3.18809(64位) 宝塔面板 下载和安装 网址:https://www.b ...
- JS中变量的命名规范
命名规范 包含数字.字母.下划线和$,但 不能以数字开头 变量名严格区分大小写 变量名不能是关键字和保留字 变量名要见名知意 如果变量名有多个单词组成,推荐使用 小驼峰命名法 命名时,尽量使用英语,如 ...