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++的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Sum Root to Leaf Numbers [LeetCode]

    Problem description: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ Basic idea: To store ...

  4. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

  5. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  6. 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 ...

  7. 【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 ...

  8. 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 ...

  9. 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 ...

  10. [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 ...

随机推荐

  1. CodeForces - 764B Timofey and cubes(模拟)

    Young Timofey has a birthday today! He got kit of n cubes as a birthday present from his parents. Ev ...

  2. c++ if语句讲解&例题

    一.if语句 1.基本语法: if(条件 布尔型){ 当条件符合执行的语句 } 2.例子: #include <iostream> using namespace std; int mai ...

  3. 解决使用tomcat服务器发布web项目时出现URL中文乱码的问题

    打开Tomcat的安装路径 打开server.xml文件 在修改端口号的一行既是下图中位置添加 URIEncoding="UTF-8" 就能替换在用eclipse或者myeclip ...

  4. Spring Boot中如何配置线程池拒绝策略,妥善处理好溢出的任务

    通过之前三篇关于Spring Boot异步任务实现的博文,我们分别学会了用@Async创建异步任务.为异步任务配置线程池.使用多个线程池隔离不同的异步任务.今天这篇,我们继续对上面的知识进行完善和优化 ...

  5. python刷题第三周

    以下是本周有所收获的题目 第一题: 第4章-4 验证"哥德巴赫猜想" (20 分) 数学领域著名的"哥德巴赫猜想"的大致意思是:任何一个大于2的偶数总能表示为两 ...

  6. mysql 添加远程访问

    第一种:mysql 5.0(两种方式) 一. 在命令窗口输入,mysql -u root -p  回车,并输入密码 执行 use mysql 执行下面句子,查看权限,root默认权限为localhos ...

  7. nginx 配置文件(支持thnkphp3.2~5)

    server { listen 8080 ; server_name localhost; set $root /var/www/myweb; #listen 443 ssl; #ssl_certif ...

  8. 鸿蒙内核源码分析(字符设备篇) | 字节为单位读写的设备 | 百篇博客分析OpenHarmony源码 | v67.01

    百篇博客系列篇.本篇为: v67.xx 鸿蒙内核源码分析(字符设备篇) | 字节为单位读写的设备 | 51.c.h.o 文件系统相关篇为: v62.xx 鸿蒙内核源码分析(文件概念篇) | 为什么说一 ...

  9. JS获取contextPath的方法

    function getContextPath() { var pathName = document.location.pathname;    var index = pathName.subst ...

  10. 【vue】两个页面间传参 - props

    目录 Step1 设置可以 props 传递数据 Step2 跳转前页面中传递数据 Step3 跳转后的页面接收数据 从 A 页面跳转到 B 页面, 参数/数据通过 props 传递到 B 页面,这种 ...