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.


题解:递归,树的高度 = max(左子树高度,右子树高度)+1;

代码如下:

 /**
* Definition for binary tree
* 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 left = maxDepth(root.left);
int right = maxDepth(root.right); return Math.max(left, right)+1;
}
}

被虐了一天,瞬间好受多了:-)

【leetcode刷题笔记】Maximum Depth of Binary Tree的更多相关文章

  1. [刷题] 104 Maximum Depth of Binary Tree

    要求 求一棵二叉树的最高深度 思路 递归地求左右子树的最高深度 实现 1 Definition for a binary tree node. 2 struct TreeNode { 3 int va ...

  2. leetcode刷题-559. Maximum Depth of N-ary Tree

    题目: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description/ n-ary-tree的数据结果表示 // Defi ...

  3. [LeetCode&Python] Problem 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. 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 l ...

  5. leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  6. [leetcode刷题笔记]Implement Trie (Prefix Tree)

    题目链接 一A,开森- ac代码: class TrieNode { // Initialize your data structure here. char content; boolean isW ...

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

  8. 【LeetCode练习题】Maximum Depth of Binary Tree

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

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

  10. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

随机推荐

  1. django模板加载静态资源

    1. 目录结构 /mysite/setting.py部分配置: # Django settings for mysite project. import os.path TEMPLATE_DIRS = ...

  2. android-support-v4.jar异常解决方法

    1.当一个项目引入其他library项目时,会出现android-support-v4.jar冲突问题: 解决:将library项目中的android-support-v4.jar更新到最新,方法右键 ...

  3. 中国程序员如何去 Facebook 工作?

    1.在Facebook,可以选择哪里工作? Facebook 在内地确实没有 Office ,但可以在https://www.facebook.com/careers/?ref=pf#location ...

  4. NumPy入门基础【2】

    通用函数ufunc 一元ufunc举例: 1.abs.fabs:计算绝对值,fabs更快 2.sqrt:计算各元素的平方根,相当于arr0.5 3.square:计算各元素的平方根,相当远arr2 4 ...

  5. Office Web Apps Server 2013与PDF(二)

    在上一篇文章(Office Web Apps Server 2013与PDF(一))中,曾经介绍了Office Web Apps Server 2013在更新后,可以直接对PDF文档进行在线的查看.不 ...

  6. 九度OJ 1345:XXX定律之画X (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:361 解决:157 题目描述: 给你一个n,然后让你输出F(n) 规则是这样的,F(n)的输出结果是: F(n-1)     F(n-1) ...

  7. jsp联合javascript操作html

    1 执行的先后顺序 jsp先处理,给页面里面的变量赋值等等.然后整个页面发送给客户端,在客户端执行javascipt相关的代码. 2 jsp文件的构成 html文件+java程序片段+jsp标签=js ...

  8. 学习使用MarkDown

    文本采用CuteMarkEd软件编写后复制到博客园(这个软件可以实时观看html效果,也可以打印pdf,挺好使.测试比sublime装插件要简单方便) MarkDown格式文本 名称 ======== ...

  9. 探测web服务器质量——pycurl

    pycurl是一个用C语言写的libcurl Python实现,功能非常强大,支持的操作协议有FTP.HTTP.HTTPS.TELNET等,可以理解为Linux下curl命令功能的Python封装,简 ...

  10. pymysql插入datetime类型

    第一种 create_time=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 第二种 update_time=time ...