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. git 回滚指定行

    Stage the parts you want with git add -p, then discard (git checkout -- filename) the unstaged chang ...

  2. 解决mapper绑定异常:nested exception is org.apache.ibatis.binding.BindingException:

    原因: 此异常的原因是由于mapper接口编译后在同一个目录下没有找到mapper映射文件而出现的.由于maven工程在默认情况下src/main/java目录下的mapper文件是不发布到targe ...

  3. kubernetes组成

    kubernetes组成 k8s主要包括: kubectl 客户端命令行工具: 将接收的命令,发送给kube-apiserver,作为对整个平台操作的入口. kube-apiserver REST A ...

  4. OAuth2.0的原理介绍

    OAuth2.0是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. OAuth2.0(开放授权)是一个正式的互联网标准协议. 允许第三方网站在用户 ...

  5. javascript 回调 继承

    var my =  function (name,fn){                                alert(name);                           ...

  6. Java项目引用外部jar包时,使用bat启动

    1.将项目导出为jar包 1)点击项目—>右击—>点击Export—>进入export页面 2)点击JAR file——>Next——>勾选项目——>选择jar包存 ...

  7. Spring Security 安全验证

    摘自:https://www.cnblogs.com/shiyu404/p/6530894.html 这篇文章是对Spring Security的Authentication模块进行一个初步的概念了解 ...

  8. Struts2重要知识点总结

    一.interceptor拦截器的使用 第一种情况(指定action使用该拦截器):struts.xml文件的配置: <interceptors> <interceptor name ...

  9. 关于Java8 Stream流的利与弊 Java初学者,大神勿喷

    题目需求: 1:第一个队伍只要名字为3个字成员的姓名,存储到新集合 2:第一个队伍筛选之后只要前3人:存储到一个新集合 3:第2个队伍只要姓张的成员姓名:存储到一个新集合 4:第2个队伍不要前2人,存 ...

  10. workman项目设置开机自启动

    https://blog.csdn.net/xxq929604980/article/details/78558317 http://man.linuxde.net/chkconfig 1.脚本编写 ...