97. Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example

Example 1:

Input: tree = {}
Output: 0
Explanation: The height of empty tree is 0.

Example 2:

Input: tree = {1,2,3,#,#,4,5}
Output: 3
Explanation: Like this:
1
/ \
2 3
/ \
4 5

注意:

Java中参数传递:(细读)

1> 基本数据类型作为参数传递时,基本类型作为参数传递时,是传递值的拷贝,无论你怎么改变这个拷贝,原值是不会改变的。

2> 对象作为参数传递时,是把对象在内存中的地址拷贝了一份传给了参数。

所以错误案例中,把基本数据类型int maxDepth作为参数,传给helper,只是拷贝了一份值给helper函数,所以无论helper里面maxDepth的值如何改变,return的值始终为1。

递归法代码(错误案例):
/**
* 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 binary tree.
* @return: An integer
*/ public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int maxDepth = 1;
helper(root, 1, maxDepth);
return maxDepth;
}
public void helper(TreeNode root, int curDepth, int maxDepth) {
if (root == null) {
return;
} if (root.left == null && root.right == null) {
if (curDepth > maxDepth) {
maxDepth = curDepth;
}
return;
}
helper(root.left, curDepth + 1, maxDepth);
helper(root.right, curDepth + 1, maxDepth);
}
}

递归法代码(改正):

/**
* 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 binary tree.
* @return: An integer
*/
int maxDepth = 1;
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
helper(root, 1);
return maxDepth;
}
public void helper(TreeNode root, int curDepth) {
if (root == null) {
return;
} if (root.left == null && root.right == null) {
if (curDepth > maxDepth) {
maxDepth = curDepth;
}
return;
}
helper(root.left, curDepth + 1);
helper(root.right, curDepth + 1);
}
}

Lintcode97-Maximum Depth of Binary Tree-Easy的更多相关文章

  1. LeetCode:104 Maximum Depth of Binary Tree(easy)

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  2. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  3. LeetCode_104. Maximum Depth of Binary Tree

    104. Maximum Depth of Binary Tree Easy Given a binary tree, find its maximum depth. The maximum dept ...

  4. [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  5. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  6. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  7. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  8. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  9. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  10. 104. Maximum Depth of Binary Tree(C++)

    104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...

随机推荐

  1. 蒜厂年会|计蒜客2019蓝桥杯省赛 B 组模拟赛(一)

    样例输入: 3 1 -2 1 样例输出: 2 方法一: 将环形数组拆分成为普通数组,(通过搬运复制数据到尾部),再求前缀和,找出最大前缀和.因为枚举了每一个起点,所以最大连续和也一定出现在前缀和中.. ...

  2. python3中报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'

    reload(sys) sys.setdefaultencoding("utf-8") f = open('.\\24.novel.txt','rb') str = f.read( ...

  3. Kafka的安装是否成功的简单测试命令

    首先了解一下kafka的基本概念 .1. BrokerKafka集群包含一个或多个服务器,这种服务器被称为broker2. Topic每条发布到Kafka集群的消息都有一个类别,这个类别被称为Topi ...

  4. Logcat

    logcat -- "-s"选项 : 设置输出日志的标签, 只显示该标签的日志; -- "-f"选项 : 将日志输出到文件, 默认输出到标准输出流中, -f 参 ...

  5. P1_jemeter安装--jdk安装

    学习的python,需要下载jemter做接口测试. 一..jMeter介绍 Apache组织开发的基于JAVA压力测试工具 100%纯JAVA开发,完全可移植性 可用于测试静态和动态资源 多协议-- ...

  6. pytorch 中的重要模块化接口nn.Module

    torch.nn 是专门为神经网络设计的模块化接口,nn构建于autgrad之上,可以用来定义和运行神经网络 nn.Module 是nn中重要的类,包含网络各层的定义,以及forward方法 对于自己 ...

  7. servlet转发重定向

    1.request.getRequestDispacther("/test.jsp").forword(request,response);     转发   浏览器URL是一个地 ...

  8. RoR - MetaProgramming

    ruby是动态语言,它有动态语言的优势与劣势 动态语言,像python与ruby 你不用提前去定义method - they need to only be "found" whe ...

  9. PTA最短工期

    一个项目由若干个任务组成,任务之间有先后依赖顺序.项目经理需要设置一系列里程碑,在每个里程碑节点处检查任务的完成情况,并启动后续的任务.现给定一个项目中各个任务之间的关系,请你计算出这个项目的最早完工 ...

  10. MAVEN_day01 下载与安装及环境变量的配置

    一.MAVEN简介 MAVEN是apache组织下的一个开源项目,是使用纯java编写的,之用于管理java工程. 二.MAVEN下载与安装 下载地址:http://maven.apache.org/ ...