Lintcode97-Maximum Depth of Binary Tree-Easy
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的更多相关文章
- 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 ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- getItemAt
getItemAt(0) 获得第一行数据 getItemAt(1) 获得第二行数据
- HTML、CSS知识点,面试开发都会需要--No.2 CSS
No.2 CSS 1.选择器类型 选择器类型包括:type.class.id. 2.引用外部css文件 使用link元素,添加rel和href属性:<link rel="styles ...
- NFC中国-中国第一NFC论坛,NFC中文论坛+NFC技术社区+NFC_电子发烧友网【申明:来源于网络】
NFC中国-中国第一NFC论坛,NFC中文论坛+NFC技术社区[申明:来源于网络] NFC中国-中国第一NFC论坛,NFC中文论坛:http://nfcchina.org/forum.php NFC技 ...
- HDU 1010生成树
求起点到终点的最短权值和
- 如何在vscode中调试python scrapy爬虫
本文环境为 Win10 64bit+VS Code+Python3.6,步骤简单罗列下,此方法可以不用单独建一个Py入口来调用命令行 安装Python,从官网下载,过程略,这里主要注意将python目 ...
- [No000017F]如何监控注册表的修改
今天我们将向您展示如何使用我们最喜欢的工具之一Proc Mon,在您更改PC上的组策略设置时查看编辑的注册表项. 使用Proc Mon查看组策略对象修改的注册表设置 您要做的第一件事就是从Sys In ...
- RoR - MetaProgramming
ruby是动态语言,它有动态语言的优势与劣势 动态语言,像python与ruby 你不用提前去定义method - they need to only be "found" whe ...
- 使用moment.js结合filter过滤器格式化时间
<td>{{item.ctime | timeFormat('yyyy-MM-dd')}}</td> //pattern = "" 形参的默认值,如果传过来 ...
- JAVA-数据库之MySQL与JDBC驱动下载与安装
相关资料:<21天学通Java Web开发> MySQL下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-wi ...
- python框架之Flask(2)-路由和视图&Session
路由和视图 这一波主要是通过看源码加深对 Flask 中路由和视图的了解,可以先回顾一下装饰器的知识:[装饰器函数与进阶] 路由设置的两种方式 # 示例代码 from flask import Fla ...