题目

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. Jquery的方法(一)

    一.文档操作1.内部插入:append(),appendTo(),prepend():2.外部插入:after(),before():3.删除操作:remove(),empty():4.克隆操作:cl ...

  2. Python 随机数函数

    random.random random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 描述 random() 方法返回随机生成的一个实数,它在[0,1)范围 ...

  3. 为了增强团队的协作和高效开发,提升代码质量,TGideas团队一起制订的代码规范。主要包括五部分内容:PC规范、移动端规范、性能优化、CP规范、其他项目规范

    http://tguide.qq.com/main/index.htm

  4. 【线性基】Petrozavodsk Winter Training Camp 2018 Day 1: Jagiellonian U Contest, Tuesday, January 30, 2018 Problem A. XOR

    题意:给你一些数,问你是否能够将它们划分成两个集合,使得这两个集合的异或和之差的绝对值最小. 设所有数的异或和为S,集合A的异或和为A. 首先,S的0的位对答案不造成影响. S的最高位1,所对应的A的 ...

  5. 【ACM-ICPC 2018 徐州赛区网络预赛】E. End Fantasy VIX 血辣 (矩阵运算的推广)

    Morgana is playing a game called End Fantasy VIX. In this game, characters have nn skills, every ski ...

  6. Codeforces Round 486C - Palindrome Transformation 贪心

    C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...

  7. Node.js学习笔记(1) - Node.js简介

    近期在看一些Node.js的知识,看完后觉得,一些前面的东西忘记了,于是整理一下,方便自己查阅,也希望对学习Node.js的朋友有些帮助: 当然以下只是我个人的观点和理解,不喜勿喷,也望大神指教. 一 ...

  8. python类型比较的3种方式(转)

    通过types模块的类成员来判断,其实所有python中的类型都是这个types模块中类型的实例. import types type(x) is types.IntType # 判断是否int 类型 ...

  9. oracle case when exists()

    用法如下: select case when exists(select 1 from t_test c where c.name = 'zhangsan'     and c.age = 23 ) ...

  10. datagrid在MVC中的运用10-勾选

    本文体验与勾选有关的特性. 需要加载的books.json 展开{ "total": 4, "rows": [ { "productid": ...