题目:计算一棵二叉树所有路径组成的数的总和。

思考:也是DFS的基础应用。虽然还是套着别人的DFS框架写的,但是学习通常会经历先模拟,再创新的过程。

代码:

     private int sum = 0;
public int sumNumbers(TreeNode root) {
dfs(root , 0);
return sum;
}
public void dfs(TreeNode node , int tempSum){
if(node == null) return ; tempSum = tempSum * 10 + node.val;
if(node.left == null && node.right == null) {
sum += tempSum;
return;
} dfs(node.left , tempSum);
dfs(node.right , tempSum);
}

[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 OJ--Sum Root to Leaf Numbers

    https://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 给一棵树,找从根到叶的路径,并把路径上的数相加,求和. 树的深度优先搜索 /** ...

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

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

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

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

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

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

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

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

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

随机推荐

  1. win10,软件, 发布者不受信任怎么办

    这个方法比较管用:右键单击windows左下角,弹出右击菜单选择‘命令提示符(管理员)(A)’,然后用DOS命令安装程序.就可以了 PS:win10的cmd可以直接复制粘贴了.

  2. 屏幕分辨率与FPS

    屏幕分辨率 刷新率分为垂直刷新率和水平刷新率,一般提到的刷新率通常指垂直刷新率. 垂直刷新率表示屏幕的图象每秒钟重绘多少次,也就是每秒钟屏幕刷新的次数,以Hz(赫兹)为单位. 刷新率越高越好,图象就越 ...

  3. 安装LINUX X86-64的10201出现链接ins_ctx.mk错误-转自yingtingkun

    详细错误信息为: Error in invoking target ‘install’ of makefile ‘/opt/oracle/product/10.2/ctx/lib/ins_ctx.mk ...

  4. Sq server 关于存储过程,触发器的一些理论简述

    http://www.doc88.com/p-2905916227462.html      

  5. C - Fractal(3.4.1)

    Description A fractal is an object or quantity that displays self-similarity, in a somewhat technica ...

  6. VC 类泡泡龙游戏算法

    #include <stdio.h> #include <malloc.h> #include <string.h> /* 1 2 1 2 2 1 2 1 2 1 ...

  7. 零基础如何入门Python

    编程零基础如何学习Python 如果你是零基础,注意是零基础,想入门编程的话,我推荐你学Python.虽然国内基本上是以C语言作为入门教学,但在麻省理工等国外大学都是以Python作为编程入门教学的. ...

  8. C#将C++动态库的回调函数封装成事件

    关于C#调用C++动态库的文章很多,调用动态库中回调函数的方法也不在少数.但大多数调用回调函数的方法依然保留了C++的语法特点. 比如有一段C++的回调函数代码,为了表达它的意思,我把注释也粘贴了进来 ...

  9. 在Ubuntu Server14.04上编译Android6.0源码

    此前编译过Android4.4的源码,但是现在Android都到了7.0的版本,不禁让我感叹Google的步伐真心难跟上,趁这周周末时间比较充裕,于是在过去的24小时里,毅然花了9个小时编译了一把An ...

  10. C/C++笔试经典程序(二)

    1.下面5个函数哪个能够成功进行两个数的交换? swap1传的是值的副本,在函数体内被修改了形参p.q(实际参数a.b的一个拷贝),p.q的值确实交换了,但是它们是局部变量,不会影响到主函数中的a和b ...