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 ...
随机推荐
- springboot 事务创建流程源码分析
springboot 事务创建流程源码分析 目录 springboot 事务创建流程源码分析 1. 自动加载配置 2. InfrastructureAdvisorAutoProxyCreator类 3 ...
- 第二十次CSP考试有感
这是第二次参加csp考试了,大二上学期参加了第17次csp,160分.刚刚下午结束了第20次csp,200分. 这次比赛规则和以往不同,以前可以携带纸质书籍和usb,提交上去的答案不能实时出成绩.现在 ...
- C++ cout格式化输出(输出格式)完全攻略
使用流操作算子 它们都是在头文件 iomanip 中定义的:要使用这些流操纵算子,必须包含该头文件. 表1:C++ 流操纵算子 流操纵算子 作 用 *dec 以十进制形式输出整数 hex 以十六进制 ...
- centos7 未启用swap导致内存使用率过高。
情况描述: 朋友在阿里云上有一台系统为CentOS7的VPS,内存为2GB,用于平时开发自己的项目时测试使用: 他在上面运行了5个docker实例,运行java程序:还有一个mysql服务: 上述5个 ...
- ESP8266- AP模式的使用
打算通过该模式,利用手机APP完成配网 • AP,也就是无线接入点,是一个无线网络的创建者,是网络的中心节点.一般家庭或办公室使用的无线路由器就是一个AP. • STA站点,每一个连接到无线网络中的终 ...
- Git 访问慢 解决办法
1. 查询Git最快的IP 通过 https://www.ipaddress.com/ 这个网站来获取当前github最新的ip分别获取以下两个域名的IP地址: 可以在访问git网站使用F12查询哪个 ...
- MSSQL数据库安全实验
管理SQL Server认证模式 (1)确认 SQL Server 验证 ①在桌面上单击"开始",选择"程序"→"Microsoft SQL Serv ...
- 深入浅出WPF-08.Event( 事件)01
事件(Event) 首先我们来继续说一下UI组件树,因为WPF事件 的路由环境就是组件树.WPF中的树有两种,一种是逻辑树(Logical Tree),一种是可视元素树(Visual Tree).逻辑 ...
- 从产业链、架构和技术三个层面,看元宇宙与RPA的发展关系
你可能还不知道,元宇宙也将带动RPA高速发展 一文读懂RPA如何赋能元宇宙,虚拟空间更需要RPA无处不在 三个层面,解读元宇宙如何利好RPA行业发展 从产业链.架构和技术三个层面,看元宇宙与RPA的发 ...
- Linux 清空日志的五种方法
VIM 是linux下一款优秀的编辑器,但是上手难度略大,网络上可以找到的教程很多,快捷键也非常多,一时很难记住. 本文换一种思路,就是根据平时自己的常用需要,去反查VIM如何操作的,再记录下来,这样 ...