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; int leftmax = maxDepth(root.left);
int rightmax = maxDepth(root.right); return leftmax>rightmax?leftmax+1:rightmax+1;
}
}

LeetCode OJ 104. Maximum Depth of Binary Tree的更多相关文章

  1. 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)

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

  2. 【一天一道LeetCode】#104. Maximum Depth of Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  3. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  4. 【LeetCode OJ】Maximum Depth of Binary Tree

    Problem Link: https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ Simply BFS from root an ...

  5. 【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 ...

  6. LeetCode OJ: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(easy)

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

  8. LeetCode之104. Maximum Depth of Binary Tree

    -------------------------------- 递归遍历即可 AC代码: /** * Definition for a binary tree node. * public clas ...

  9. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

随机推荐

  1. Android: Failure [INSTALL_FAILED_DEXOPT] and Failure [INSTALL_FAILED_UID_CHANGED] 解决方案

    1. 错误:  Failure [INSTALL_FAILED_DEXOPT]  Android安装App时 D:\WorkSpace\Administrator\workspace\svn\soot ...

  2. HTML5入门总结 HTML5API

    w3cshools  MDN英文  MDN中文 HTML5 HTML5 is the latest evolution of the standard that defines HTML. The t ...

  3. C语言中sprintf()函数的用法

    sprintf函数的用法 1.该函数包含在stdio.h的头文件中. 2.sprintf和平时我们常用的printf函数的功能很相似.sprintf函数打印到字符串中,而printf函数打印输出到屏幕 ...

  4. jquery实现图片漂浮效果

    $(window).load(function(){   function moveRocket(){   $("#float").animate({'left':'+=100'} ...

  5. C语言 · 数的统计

    问题描述 在一个有限的正整数序列中,有些数会多次重复出现在这个序列中. 如序列:3,1,2,1,5,1,2.其中1就出现3次,2出现2次,3出现1 次,5出现1次. 你的任务是对于给定的正整数序列,从 ...

  6. lt&gt&eq

    lt:less than,小于 gt:greater than,大于 eq:equal,等于 le:less equal,小于等于 ge:greater than,大于等于

  7. iOS毛玻璃擦除效果

    思路:首先我的思路放两个imageView在Controller里把高清的图片放在下面,带有毛玻璃效果的图片放在上面. //在Controller的view上加一个手势代码如下(温馨提示,开启imae ...

  8. thinkphp u 方法

    public function test(){ $this->display();echo U('Index/test',array('id'=>1),false,'localhost') ...

  9. javascript 拖放效果

    最近一直在看javascript的书籍,有些东西在书上看着貌似理解了,但是在真正动手实践时,其实有些细节你根本不了解.所以看起来就算是一个简单的效果,写起来也未必简单,就算写起来简单,写的代码也未必规 ...

  10. java 读取excel文件(只读取xls文件)

    package com.sun.test; import java.io.BufferedInputStream;import java.io.File;import java.io.FileInpu ...