Leetcode Sum Root to Leaf Numbers

时间:2021-07-05 00:51:21

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

An example is the root-to-leaf path 1->2->3 which represents the number 123.

Find the total sum of all root-to-leaf numbers.

For example,

    1
/ \
2 3

The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.

本题利用利用深搜到叶子节点,同时记录下sum即可

int dfs(TreeNode *root, int sum){
if(root == NULL) return ;
sum = root->val+sum*;
if(root->left == NULL && root->right == NULL) return sum;
return dfs(root->left,sum)+dfs(root->right,sum);
} int sumNumbers(TreeNode *root){
if(root == NULL ) return ;
int sum = root->val;
if(root-> left == NULL && root->right ==NULL) return sum; return dfs(root->left,sum) + dfs(root->right, sum);
}

Leetcode Sum Root to Leaf Numbers的更多相关文章

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

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

  3. [leetcode]Sum Root to Leaf Numbers @ Python

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

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

  5. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

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

  6. LeetCode :: Sum Root to Leaf Numbers [tree、dfs]

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

  7. [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

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

  8. LeetCode Sum Root to Leaf Numbers(DFS)

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

  9. leetcode Sum Root to Leaf Numbers(所有路径之和)

    转载请注明来自souldak,微博:@evagle 观察题目给的返回值类型是int,可以断定这棵树的高度不会超过10,所以数据量其实是非常小的.那就直接dfs遍历这棵树,然后到叶子节点的时候将值加到最 ...

随机推荐

  1. UVALive 4426 Blast the Enemy! --求多边形重心

    题意:求一个不规则简单多边形的重心. 解法:多边形的重心就是所有三角形的重心对面积的加权平均数. 关于求多边形重心的文章: 求多边形重心 用叉积搞一搞就行了. 代码: #include <ios ...

  2. docker 私有镜像管理工具harbor 安装

    因为各种原因,官方的离线安装包下载比较费事,经常不成功,所以通过分部安装解决问题 1. docker yum install libdevmapper* -y -H tcp://0.0.0.0:237 ...

  3. 一次GC问题定位

    同事有段代码执行时间过长,需要进行优化, Hashmultimap<Int,Bean> map = ...; for (400w*96) { // 计算过程 Bean = doComput ...

  4. App性能优化

    http://www.cocoachina.com/ios/20150429/11712.html http://blog.csdn.net/jasonblog/article/details/765 ...

  5. 打印手机当前界面(位于栈顶)的activity

    adb shell dumpsys activity activities | grep "Hist #0" 一般第一条就是当前页(位于栈顶)的activity

  6. shell脚本编写实例

    实际案例 1.判断接收参数个数大于1 [ $# -lt 1 ] && echo "至少需要一个参数" && { echo "我要退出了.. ...

  7. 浅析C&num;中的IEquatable&lt&semi;T&gt&semi;接口

    1.引言 首先我们先来看看IEquatable<T>接口的出现解决了什么问题. 我们知道,Object基类的Equals方法存在两个明显的问题.一是缺乏类型安全性,二是对于值类型而言需要装 ...

  8. Android 屏幕密度适配

    Android Icon Size and Location for Apps   分辨率 DPI Density scale 1dp对应像素 1dp对应物理尺寸 Location Icon Size ...

  9. C中atoi和strcpy的自定义实现

    这是两道经常考到的笔试题,看似简单的实现,其实专注到细节,还是有很多需要注意扣分的地方. atoi实现: #include <iostream> #include<ctype.h&g ...

  10. nyoj-115-城市平乱(dijkstra算法)

     题目链接 /* Name:nyoj-115-城市平乱 Copyright: Author: Date: 2018/4/25 17:28:06 Description: dijkstra模板题 枚举从 ...