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. 关于vue-cli创建项目(小白)(2)mock数据

    mock数据,好处,前后端分离,不用等后端的真实接口,就可以用axios(ek sju s 好像这么读,原谅本人总是根据读音写单词)请求数据了. 一,安装所需插件 根据不同需求选择安装环境,mockj ...

  2. 浅谈SFTP和FTP的区别

    一.适用场景 我们平时习惯了使用ftp来上传下载文件,尤其是很多Linux环境下,我们一般都会通过第三方的SSH工具连接到Linux,但是当我们需要传输文件到Linux服务器当中,很多人习惯用ftp来 ...

  3. eclipse中的快捷键的使用

  4. jquery 在页面上根据ID定位(jQuery锚点跳转及相关操作) 经典

    1.锚点跳转简介 Edit 锚点其实就是可以让页面定位到某个位置上的点.在高度较高的页面中经常见到.比如百度的百科页面,wiki中的page内容. 我知道实现锚点的跳转有两种形式,一种是a标签+nam ...

  5. webservice接口测试wsdl

    http和webservice接口测试有什么区别? webservice的基础组成是http+xml 三要素:soap传输协议,uddi,wsdl(webservice描述语言xml格式) 优点:跨平 ...

  6. 转 docker创建私有仓库和k8s中使用私有镜像

    docker私有仓库建立 环境说明我们选取192.168.5.2做私有仓库地址yum install docker -y1.启动docker仓库端口服务 docker run -d -p 5000:5 ...

  7. Visual Studio 2017使用Asp.Net Core构建Angular4应用程序

    文章转载请著名出处:http://www.cnblogs.com/smallprogram 你需要了解的名词 1. NodeJS,这是一个基于Chrome V8 JavaScript引擎构建的Java ...

  8. python进阶之 进程编程

    1.进程 顾名思义,进程即正在执行的一个过程.进程是对正在运行程序的一个抽象. 进程的概念起源于操作系统,是操作系统最核心的概念,也是操作系统提供的最古老也是最重要的抽象概念之一.操作系统的其他所有内 ...

  9. IO实时监控命令iostat详解

    iostat用于输出CPU和磁盘I/O相关的统计信息 命令格式 iostat [ -c ] [ -d ] [ -h ] [ -N ] [ -k | -m ] [ -t ] [ -V ] [ -x ] ...

  10. Clone of COCO API

    Clone of COCO API - Dataset @ http://cocodataset.org/ - with changes to support Windows build and py ...