481. Binary Tree Leaf Sum

Given a binary tree, calculate the sum of leaves.

Example

Example 1:

Input:
1
/ \
2 3
/
4
output:7.

Example 2:

Input:
1
\
3
Output:3.

注意:
sum是类成员变量的原因:因为没执行一次递归,递归方法里的局部变量都会被重新创建。在递归函数中,不需要每次都创建sum,只有访问叶子结点时,对sum操作即可。

递归过程详解(讲解很透彻,包括递归中的return过程,递归的终止条件)
理解递归,关键是脑中有一幅代码的图片,函数执行到递归函数入口时,就扩充一段完全一样的代码,执行完扩充的代码并return后,继续执行前一次递归函数中递归函数入口后面的代码

递归法代码:

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param root: the root of the binary tree
* @return: An integer
*/
int sum = 0;
public int leafSum(TreeNode root) {
helper(root);
return sum;
}
public void helper(TreeNode root) {
if (root == null) {
return;
} if (root.left == null && root.right == null) {
sum += root.val;
return;
} helper(root.left);
helper(root.right);
}
}

Lintcode481-Binary Tree Leaf Sum-Easy的更多相关文章

  1. [LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

    Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this p ...

  2. Binary Tree Path Sum

    Given a binary tree, find all paths that sum of the nodes in the path equals to a given number targe ...

  3. 376. Binary Tree Path Sum【LintCode java】

    Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...

  4. leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree

    1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...

  5. [算法专题] Binary Tree

    1 Same Tree https://leetcode.com/problems/same-tree/ Given two binary trees, write a function to che ...

  6. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  7. 二叉树中的最大路径和 · Binary Tree Maximum Path Sum

    [抄题]: 给出一棵二叉树,寻找一条路径使其路径和最大,路径可以在任一节点中开始和结束(路径和为两个节点之间所在路径上的节点权值之和) [思维问题]: 不会写分合法 [一句话思路]: 用两次分治:ro ...

  8. [LeetCode] 124. Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  9. [leetcode]Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

随机推荐

  1. table中内容过长,table改变的问题

    在看效果时发现在Chrome中的table已经不是原来设置的宽度了,而其他浏览器是好的,经过百度发现是单元格内容过多造成的,但这时候给td设置宽度已经不适用了,此时就要给table设置 table{t ...

  2. 二、JavaScript基础(2)

    BOM基础加强 1.浏览器对象BOM DOM Window DOM Navigator DOM Screen DOM History DOM Location 2.浏览器对象的使用 History H ...

  3. 剑指offer——python【第21题】栈的压入、弹出序列

    题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...

  4. 如果报错,使用 journalctl -f -t etcd 和 journalctl -u etcd 来定位问题。

    如果报错,使用 journalctl -f -t etcd 和 journalctl -u etcd 来定位问题.

  5. 强类型Dataset使用事务(改进原有方法)

    以下部份转自:http://blog.csdn.net/nfbing/article/details/5803980 关于强类型Dataset的用法和好处,我就不再多说,网上关于这方面的资料很多 , ...

  6. JQuery实现表格自动增加行,对新行添加事件

    实现功能: 通常在编辑表格时表格的行数是不确定的,如果一次增加太多行可能导致页面内容太多,反应变慢:通过此程序实现表格动态增加行,一直保持最下面有多个空白行. 效果: 一:原始页面 二:表1增加新行并 ...

  7. day15 十五、模块、from导入、起别名

    一.模块的概念 1.什么是模块:一系列功能的集合体 2.定义模块:创建一个py文件就是一个模块,该py文件名就是模块名 模块的四种存在方式 使用python编写的.py文件 包:一堆py文件的集合体 ...

  8. xcode代码提示没了

    defaults write com.apple.dt.XCode IDEIndexDisable 0 https://www.jianshu.com/p/57a14bed9d1b

  9. mysql-proxy 简介

    # 背景 今天同事分享的主题就是mysql-proxy,于是下来自己了解下,不求精通,只求知道这个玩意 # 简介 mysql-proxy是mysql官方提供的mysql中间件服务,上游可接入若干个my ...

  10. Ext 修改内容之后 不做任何动作 再次修改时的数据是原来第一次修改前的数据

    转自  http://blog.csdn.net/jaune161/article/details/18220257 在项目开发中遇到这样一个问题,点击Grid中的一条记录并修改,修改完后保存并且刷新 ...