题目

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.

题解

递归解法:

     public int maxDepth(TreeNode root) {
         if(root==null)
             return 0;
         int leftmax = maxDepth(root.left);
         int rightmax = maxDepth(root.right);
         return Math.max(leftmax, rightmax)+1;
     }

非递归解法,参考codeganker(http://codeganker.blogspot.com/2014/02/maximum-depth-of-binary-tree-leetcode.html)的:

 1 public int maxDepth(TreeNode root) {
 2     if(root == null)
 3         return 0;
 4     
 5     int depth = 0;
 6     LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
 7     queue.add(root);
 8     int curNum = 1; //num of nodes left in current level
 9     int nextNum = 0; //num of nodes in next level
     while(!queue.isEmpty()){
         TreeNode n = queue.poll();
         curNum--;
         if(n.left!=null){
             queue.add(n.left);
             nextNum++;
         }
         if(n.right!=null){
             queue.add(n.right);
             nextNum++;
         }
         if(curNum == 0){
             curNum = nextNum;
             nextNum = 0;
             depth++;
         }
     }
     return depth;
 }

Maximum Depth of Binary Tree leetcode java的更多相关文章

  1. Maximum Depth of Binary Tree leetcode

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

  2. Minimum Depth of Binary Tree leetcode java

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

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

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

    104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...

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

  6. LeetCode——Maximum Depth of Binary Tree

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

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

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

随机推荐

  1. 【知了堂学习笔记】java 编写几种常见排序算法2

    排序的分类: 1.直接选择排序 它的基本思想是:第一次从R[0]~R[n-1]中选取最小值,与R[0]交换,第二次从R[1]~R[n-1]中选取最小值,与R[1]交换,....,第i次从R[i-1]~ ...

  2. 统计无向图中三角形的个数,复杂度m*sqrt(m).

    统计无向图中三角形的个数,复杂度m*sqrt(m). #include<stdio.h> #include<vector> #include<set> #inclu ...

  3. python 与 mongodb的交互

  4. BigDecimal 两种方式

    第一种: Double a=0.06; Double b=0.01; BigDecimal addend = BigDecimal.valueOf(a); BigDecimal augend = Bi ...

  5. NetCore+Dapper WebApi架构搭建(四):仓储的依赖注入

    上一节我们讲到实体,仓储接口和仓储接口的实现需要遵循约定的命名规范,不仅是规范,而且为了依赖注入,现在我们实现仓储的依赖注入 在NetCore WebApi项目中新添加一个文件夹(Unit),当然你也 ...

  6. iOS 9应用开发教程之显示编辑文本标签文本框

    iOS 9应用开发教程之显示编辑文本标签文本框 ios9显示.编辑文本 在iOS,经常会看到一些文本的显示.文字就是这些不会说话的设备的嘴巴.通过这些文字,可以很清楚的指定这些设备要表达的信息.本节将 ...

  7. python脚本 pyqt 打包成windows可执行exe文件 pyinstaller

    今天学习pyqt,做了一些好玩的东西. 好奇之中想试试python脚本编译成可执行文件,一顿查询之后成功了! 我的环境是: windows10 64bit    python3.5    pyqt5 ...

  8. Java输入输出入门 A+B

    描述 求两个整数之和. 输入 输入数据只包括两个整数A和B. 输出 两个整数的和. 样例输入 1 2 样例输出 3 import java.util.Scanner; public class Mai ...

  9. PostgreSQL教程收集(中文文档/命令行工具/常用命令)

    http://www.postgres.cn/docs/9.6/index.html(中文文档) https://www.postgresql.org/docs/10/static/auth-meth ...

  10. svn提交代码忘写注释怎么办,我想补充上去?

    propset --revprop -r 24288--force "svn:log" "一级采购人在填写申报书,汇总批量删除二级采购人申报书时报错" http ...