题目描述:

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.

解题思路:

分左子树和右子数递归调用。

代码如下:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
if(root == null)
return 0;
else
return Math.max(maxDepth(root.left) + 1, maxDepth(root.right) + 1);
}
}

  

Java [Leetcode 104]Maximum Depth of Binary Tree的更多相关文章

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

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

  2. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

  3. [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

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

  4. (二叉树 BFS DFS) leetcode 104. 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 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java

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

  6. Java for LeetCode 104 Maximum Depth of Binary Tree

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

  7. leetcode 104 Maximum Depth of Binary Tree ----- java

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

  8. LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)

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

  9. LeetCode 104. Maximum Depth of Binary Tree

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

随机推荐

  1. WPF xmal绑定数据,当显示数据过长用省略号代替的方法

    有时候会遇到这种情况,用了数据绑定显示的数据太长时,如何让过长的数据显示规定的长度,多余的用省略号代替呢,自己写了个简单的小例子和大家分享一下^_^,我也是学习WPF不久,这是我第一次写博客,有问题还 ...

  2. PHP内置函数

    1 变量函数 a.is_xxx函数用来判断变量类型       is_int.is_integer.is_long,判断变量是否是整型       is_float.is_double.is_real ...

  3. eclipse 僵死/假死 问题排查及解决

    症状: 使用Eclipse win 64位版本,indigo及kepler都重现了,使用tomcat 6.0.39,jdk1.6.u45及1.7u45均尝试了,也重现. 重现步骤很简单,使用debug ...

  4. python logging 日志轮转文件不删除问题

    前言 最近在维护项目的python项目代码,项目使用了 python 的日志模块 logging, 设定了保存的日志数目, 不过没有生效,还要通过contab定时清理数据. 分析 项目使用了 logg ...

  5. mac 如何让文件隐藏

    1.首先,要确保知道目标文件或文件夹的名称,你不把这个名称正确地输入到终端中,Mac也无能为力啊... 2.打开终端,输入chflags hidden 3.在上述代码的后面加上该文件夹的路径,但是注意 ...

  6. SpringMVC注解@RequestParam(转)

    鸣谢:http://shawnccx.iteye.com/blog/730239 -------------------------------------------------- 在SpringM ...

  7. IOS 数组分组 Grouped NSArray

    NSMutableSet *set=[NSMutableSet set]; [_list enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BO ...

  8. JS中如何定义全局变量

    三种方法 1.在js的function外定义一个变量 var name='测试'; function XX(){        alert(name); } 2.不使用var,直接给定义变量,隐式的声 ...

  9. leetcode6 Reverse Words in a String 单词取反

    Reverse Words in a String  单词取反 whowhoha@outlook.com Question: Given an input string s, reverse the ...

  10. Coder-Strike 2014 - Round 1(A~E)

    题目链接 A. Poster time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputou ...