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.

思路:每条root到叶节点的路径代表着一个数。沿着路径,各节点的值在数中由高位到低位,这个有点像在数组中,数组A[]的元素是0~9的整数,求由整个数组元素构成整数,方法是定义变量sum=0,然后sum=sum*10+A[i]。对应一条路径去看,也可以同样去做,只不过这里当前元素的值为当前节点所对应的值。对整棵二叉树,是左子树所有路径构成整数的和加上右子树。考虑到用递归(DFS)来解,遇到两种情况,一、如何处理节点:空节点、叶节点、一般节点;二、如何求和。

针对问题一,遇到空节点,说明其没有左右孩子了,而且对应的值为0;遇到一般节点,则由sum直接加上当前节点的值;遇到叶节点,说明递归到最底端了,本次的递归结束,返回sum,即递归条件构造好了;针对问题二,类似数组的问题,对整棵二叉树节点值得和

可以写为sum(左子树)+sum(右子树),则递归公式确定了。对于递归算法,我也是一知半解,打算通过做题来熟悉,欢迎大神支招!

 /**
* 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)
{
int sum=;
return sumSubtree(root,sum);
} int sumSubtree(TreeNode *root,int sum)
{
if(root==NULL) return ; //不存在,则认为值为0
sum=sum*+root->val; if(root->left==NULL&&root->right==NULL) //到达叶节点
return sum; return sumSubtree(root->left,sum)+sumSubtree(root->right,sum);
}
};

[Leetcode] Sum root to leaf numbers求根到叶节点的数字之和的更多相关文章

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

  2. [LeetCode] 129. Sum Root to Leaf Numbers 求根到叶节点数字之和

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  3. LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...

  4. Leetcode129. Sum Root to Leaf Numbers求根到叶子节点数字之和

    给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字. 例如,从根到叶子节点路径 1->2->3 代表数字 123. 计算从根到叶子节点生成的所有 ...

  5. 129 Sum Root to Leaf Numbers 求根叶数字总和

    给定一个只包含 0-9 数字的二叉树,每个根到叶的路径可以代表一个数字.例如,从根到叶路径 1->2->3则代表数字 123.查找所有根到叶数字的总和.例如,    1   / \  2  ...

  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]Sum Root to Leaf Numbers @ Python

    原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...

  8. LeetCode Sum Root to Leaf Numbers(DFS)

    题意: 给一棵二叉树,每个节点上有一个数字,范围是0-9,将从根到叶子的所有数字作为一个串,求所有串的和. 思路: 普通常规的DFS. /** * Definition for a binary tr ...

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

随机推荐

  1. dubbo入门(一)

    1.简介 Dubbo由阿里巴巴开源,是一个分布式服务框架,致力于提供高性能和透明化的RPC(远程过程调用)远程服务调用方案,以及SOA服务治理方案.如果没有分布式的需求,Dbubbo是不需要的,其本质 ...

  2. 「日常训练」Regular Bridge(Codeforces Round 306 Div.2 D)

    题意与分析 图论基础+思维题. 代码 #include <bits/stdc++.h> #define MP make_pair #define PB emplace_back #defi ...

  3. HTTP请求中get和post的区别是什么

    GET和POST是Http请求中最常用的两种请求方法 首先介绍GET与POST的差异: (1)GET请求资源数据,POST向服务器传递需要处理的数据 (2)GET传递数据大小不超过2kb,POST没有 ...

  4. [CodeForce721C]Journey

    题目描述 Recently Irina arrived to one of the most famous cities of Berland - the Berlatov city. There a ...

  5. Java学习 · 初识 多线程

    多线程 1. 基础概念 a)     程序 Program i.           静态代码,指令集,应用程序执行的蓝本 b)    进程 Process i.           动态概念,正在运 ...

  6. spark相关脚本解析

    spark-shell/spark-submit/pyspark等关系如下: #spark-submit 逻辑: ########################################### ...

  7. solidity python 签名和验证

    注意,以太坊智能合约里面采用的是公钥非紧凑类型 def gen_secrets_pair(): """ 得到公钥和私钥 :return: ""&quo ...

  8. 又见CLOSE_WAIT

    原文: http://mp.weixin.qq.com/s?__biz=MzI4MjA4ODU0Ng==&mid=402163560&idx=1&sn=5269044286ce ...

  9. 【IdentityServer4文档】- 欢迎来到 IdentityServer4

    欢迎来到 IdentityServer4 IdentityServer4 是一款包含和实现了 OpenID Connect 和 OAuth 2.0 协议的,适用于 ASP.NET Core 的框架 . ...

  10. PHP利用pcntl_exec突破disable_functions

    http://fuck.0day5.com/?p=563 PHP突破Disable_functions执行Linux命令 利用dl函数突破disable_functions执行命令 http://ww ...