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. windows下eclipse实现操作虚拟机ubantu中的hdfs hbase

    1.首先打开虚拟机,查看虚拟机的ip地址 2.修改C:\Windows\System32\drivers\etc下的主机名与ip的映射文件 3.配置Map/reduce 配置成功后可以查看hdfs文件 ...

  2. Oracle课程档案,第八天

    存储管理 查询块的大小:show parameter db_block_size database:数据库 tablespace:表空间 datafile:数据文件 segments:段 extent ...

  3. InnoDB中锁的模式

    Ⅰ.总览 S行级共享锁 lock in share mode X行级排它锁 增删改 IS意向共享锁 IX意向排他锁 AI自增锁 Ⅱ.锁之间的兼容性 兼 X IX S IS X × × × × IX × ...

  4. day5_不能循环删除list-深拷贝、浅拷贝(import copy)

    一.循环删list里面的元素,会导致下标错位,结果是不对的举例:想删除奇数 l = [1,1,1,2,3,4,5] for i in l: if i%2 !=0: l.remove(i) #删除后,导 ...

  5. IE浏览器Web自动化

    一.常见问题 1.1  在IE11浏览器下运行自动化脚本特别缓慢. 具体表现:(64位IE驱动器下)脚本运行慢,尤其是文本框输入,其它浏览器是一起输入,但IE是单个字节输入字符串 解决方法:更换IE的 ...

  6. Spring Boot核心注解@SpringBootApplication

    一.作用   @SpringBootApplication是一个组合注解,用于快捷配置启动类. 二.用法   可配置多个启动类,但启动时需选择以哪个类作为启动类来启动项目. 三.拆解 1.拆解     ...

  7. Python cffi学习(二)

    上篇文章中讲到Python中调用外部c文件可以有两种方法,一是使用cffi.verify()的形式使用,但是该种方式仍然需要进行函数声明.二是将外部c文件编译成为函数库,然后通过cffi进行使用. 由 ...

  8. MAVEN day04 SSH之分模块开发

    一.创建父工程 1.选择>>"Maven Project"创建Maven工程.并且选择Packaging为 POM. 创建父工程主要是让子工程区继承父工程,减少冗余,多 ...

  9. java+tomcat开发环境搭建

    java+tomcat开发环境搭建 一.jdk环境变量设置 ...........这里省略n个字............. 二.tomcat环境变量设置 安装好tomcat后 1.新建环境变量: CA ...

  10. docker单机网络类型

    docker单机网络类型概述 Docker 安装时会自动在 host 上创建三种网络  分别为 bridge    host   none .   可用 docker network ls 命令查看 ...