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. C语言基础知识【环境设置】

    直接使用绿色版的VC++6.0就ok,后期我会写一个具体的使用教程

  2. static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别?

    答案:全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量.全局变量本身就是静态存储方式,静态全局变量当然也是静态存储方式. 这两者在存储方式上并无不同.这两者的区别虽在于非静态全 ...

  3. 【BZOJ3331】[BeiJing2013]压力 Tarjan求点双

    [BZOJ3331][BeiJing2013]压力 Description 如今,路由器和交换机构建起了互联网的骨架.处在互联网的骨干位置的核心路由器典型的要处理100Gbit/s的网络流量.他们每天 ...

  4. Python PhatomJS 和Selenium动态加载页面 获取图片内容

    如果您觉得感兴趣的话,可以添加我的微信公众号:一步一步学Python![](http://images2017.cnblogs.com/blog/993869/201711/993869-201711 ...

  5. 搭建Cat笔记01

    昨天晚上搭建Cat 时候那叫一个坑b,宝宝心里苦呀! 准备工作: 1.先大众点评Cat的项目源码,https://github.com/dianping/cat.git 2.打包编译: mvn cle ...

  6. iOS OC和JS的交互 javaScriptCore方法封装

    一.javaScriptCore javaScriptCore是一种JavaScript引擎,主要为webKit提供脚本处理能力,javaScriptCore是开源webkit的一部分,他提供了强大的 ...

  7. [luogu4255]公主の#18文明游戏

    [luogu4255]公主の#18文明游戏 luogu 发现没有连边,只有删边? 考虑倒着做 开map记M[i][j]表示编号为i的并查集,信仰j的人数 s[i]表示编号为i的并查集的总人数 首先询问 ...

  8. 11.Django数据库操作(查)

    django.db.models.query.QuerySet1.可迭代2.可切片 官方文档:https://docs.djangoproject.com/en/1.9/ref/models/quer ...

  9. java上传文件,下载文件

    1.上传文件 1 protected int doTask(BaseForm form) throws AppException, FatalException, NoExistsException, ...

  10. spring mvc实现Restful返回xml格式数据

    最近,想在自己的小项目中搭建一个Restful风格的服务接口api,项目用的spring mvc 3,听说spring mvc本身就能十分方便的支持restful的实现,于是查询了下资料,果然非常强大 ...