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 ...
随机推荐
- 完全分布式Hadoop2.X的搭建
准备工作: 安装jdk 克隆2台虚拟机完成后:新的2台虚拟机,请务必依次修改3台虚拟机的ip地址和主机名称[建议三台主机名称依次叫做:master.node1.node2 ](虚拟机的克隆,前面的博客 ...
- MySql分区、分表和分库
MySql分区.分表和分库 数据库的数据量达到一定程度之后,为避免带来系统性能上的瓶颈.需要进行数据的处理,采用的手段是分区.分片.分库.分表. 一些问题的解释: 1.为什么要分表和分区? 日常开发中 ...
- 在eclipse上配置tomcat(包括解决找不到server,配置8.0以上版本)
下载安装eclipse普通eclipse最多只支持到tomcat v 7,要想使用8以上的tomcat,就需要下载最新版本的Eclipse IDE,安装时 选择 Eclipse IDE for Ent ...
- windows安装python2.7、python3.7和pycharm
下载安装包 下载可执行文件 安装 安装2.7 安装pycharm
- php socket 发送http请求 GET POST
http://docs.php-http.org/en/latest/httplug/users.html <?php /** * Created by PhpStorm. * User: Mc ...
- php-抽象工厂
目标:创建有依赖关系的实例;(套餐) <?php //抽象类 食物 interface IAllayFood { function Allay(); } interface IDrinkFood ...
- PHP验证
class yanzhenglei{ /** * 检查日期格式 * @param string $str 日期格式2015-01-01 * @return bool ...
- [转载]Windows 2008多用户同时远程登陆配置方法
有些朋友需要在在使用Windows 2008远程登录功能时,进行多用户登录,那么就可以采用以下配置方法: 首先要启用远程桌面这一功能:右击"我的电脑"→ 属性 → 远程配置 → 远 ...
- PolarDB PostgreSQL 快速入门
什么是PolarDB PostgreSQL PolarDB PostgreSQL(下文简称为PolarDB)是一款阿里云自主研发的云原生数据库产品,100%兼容PostgreSQL,采用基于Share ...
- 查看显卡报错:NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.
当输入nvidia-smi时出现 NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make ...