题目:

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 binary tree

* public class TreeNode {

*     int val;

*     TreeNode left;

*     TreeNode right;

*     TreeNode(int x) { val = x; }

* }

*/

import java.util.*;

public class Solution {

public int maxDepth(TreeNode root) {

if(root==null)

return 0;

if(root.left==null&&root.right==null)

return 1;

方法一:递归

/*    if(root.left==null)

return maxDepth(root.right)+1;

if(root.right==null)

return maxDepth(root.left)+1;

int left=maxDepth(root.left);

int right=maxDepth(root.right);

return (left>right)?(left+1):(right+1);

*/

//方法二:层序遍历结束,每一层level加一

Queue<TreeNode> q=new LinkedList<>();

q.add(root);

int level=0;

while(!q.isEmpty()){

int size=q.size();

level++;

for(int i=0;i<size;i++){

TreeNode node=q.poll();

if(node.left!=null)

q.add(node.left);

if(node.right!=null)

q.add(node.right);

}

}

return level;

}

}

maximun-depth-of-binary-tree的更多相关文章

  1. 【LeetCode练习题】Minimum Depth of Binary Tree

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

  2. [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度

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

  3. [LeetCode] 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. [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 ...

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

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

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

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

  9. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

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

随机推荐

  1. 详解EBS接口开发之更新供应商付款方法

    更新供应商地点层的付款方法API DECLARE --API 参数 l_external_payee_rec_type iby_disbursement_setup_pub.external_paye ...

  2. Jeff Atwood:软件工程已死?

    原文作者:Jeff Atwood 2009年7月,Tom DeMarco在<IEEE Software>杂志上发表了一篇论文,题为"Software Engineering: A ...

  3. Androd选取相册照片和拍照处理-android学习之旅(62)

    实现如下图所示效果 核心代码 -构建打开相册和拍照的Intent 拍照 File outputImage = new File(Environment.getExternalStorageDirect ...

  4. HTML5 预加载

    原文地址: HTML5 Link Prefetching 原文日期: 2010年07月07日 翻译日期: 2013年08月13日 浏览器厂商和开发者之间共同努力的一个方向就是让网站更快.现在已有很多广 ...

  5. Dynamics CRM 2013 停用和激活按钮的显示与隐藏

    CRM中命令栏上的有些按钮是可以通过权限控制显示和隐藏的,比如新建.保存.保存并关闭.删除等,但惟独激活和停用无法控制,但我们还是可以用权限去控制,只是稍微绕了那么一下. 这里就要涉及到按钮的自定义了 ...

  6. Java关键字之this

    this的作用: 1) this是当前对象的一个引用,便于对当前对象参数的使用: 2)可以返回对象的自己这个类的引用,同时还可以在一个构造函数当中调用另一个构造函数 this示例: public cl ...

  7. libevent之event

    就如libevent官网上所写的“libevent - an event notification library”,libevent就是一个基于事件通知机制的库,可以看出event是整个库的核心.e ...

  8. android 打造不同的Seekbar

    最近项目需要用到双向的seekbar,网上找了好多野不能达到要求,偶然一次机会看到了大众点评的例子,然后我最他做了优化,并对常用的seekbar做了总结. 向上两张图: 比如双向seekbar pub ...

  9. how tomcat works 五 servlet容器 上

    servlet容器是用来处理请求servlet资源,并为Web客户端填充response对象的模块.在上一篇文章(也就是书的第四章)我们设计了SimpleContainer类,让他实现Containe ...

  10. Linux内核中SPI总线驱动分析

    本文主要有两个大的模块:一个是SPI总线驱动的分析 (研究了具体实现的过程): 另一个是SPI总线驱动的编写(不用研究具体的实现过程). 1 SPI概述 SPI是英语Serial Peripheral ...