给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶节点的最长路径上的节点数。
案例:
给出二叉树 [3,9,20,null,null,15,7],
    3
   / \
  9  20
    /  \
   15   7
返回最大深度为 3 。
详见:https://leetcode.com/problems/maximum-depth-of-binary-tree/description/

Java实现:

递归实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
int left=maxDepth(root.left);
int right=maxDepth(root.right);
return left>right?left+1:right+1;
}
}

非递归实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
LinkedList<TreeNode> que=new LinkedList<TreeNode>();
que.offer(root);
int node=1;
int level=0;
while(!que.isEmpty()){
root=que.poll();
--node;
if(root.left!=null){
que.offer(root.left);
}
if(root.right!=null){
que.offer(root.right);
}
if(node==0){
++level;
node=que.size();
}
}
return level;
}
}

104 Maximum Depth of Binary Tree 二叉树的最大深度的更多相关文章

  1. [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 long ...

  2. LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java

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

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

  4. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

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

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

  7. Leetcode 104 Maximum Depth of Binary Tree 二叉树

    计算二叉树的最大深度 我的方法是找出两个子树的长度中最长的那个,然后加1 class Solution { public: int maxDepth(TreeNode* root) { ; ,maxD ...

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

  9. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

随机推荐

  1. Gym - 101147E E. Jumping —— bfs

    题目链接:http://codeforces.com/gym/101147/problem/E 题意:当人在第i个商店时,他可以向左或向右跳di段距离到达另一个商店(在范围之内),一个商店为一段距离. ...

  2. HTML5 实现文件拖放上传

    1. [图片] 5375acf5gw1dusqsscfksj.jpg ​2. [代码][HTML]代码 <!DOCTYPE html><html lang="en" ...

  3. uva 401 Palindromes 解题报告

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. spring mvc提交日期类型参数

    如题,spring mvc直接提交Date类型参数会报错,400 bad request的错误.在controller里加上 @InitBinder protected void initBinder ...

  5. hdu-5762 Teacher Bo(抽屉原理+暴力)

    题目链接: Teacher Bo Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  6. hdu-4991 Ordered Subsequence(dp+树状数组)

    题目链接: Ordered Subsequence Time Limit: 4000/2000 MS (Java/Others)     Memory Limit: 32768/32768 K (Ja ...

  7. PIL数据和numpy数据的相互转换

    在做图像处理的时候,自己常用的是将PIL的图片对象转换成为numpy的数组,同时也将numpy中的数组转换成为对应的图片对象. 这里考虑使用PIL来进行图像的一般处理. from PIL import ...

  8. AOP原理

    AOP(面向切面编程):扩展功能时不修改源代码,采用横向抽取机制 纵向抽取机制: 横向抽取机制: 第一种情况: 第二种情况:

  9. 在浏览器上直接输入 http://www.bookEstore.com就可以访问工程问题

    关于在浏览器上直接输入 http://www.bookEstore.com就可以访问工程问题 1.在tomcat/conf/server.xml文件中配置一个虚拟主机 <Host name=&q ...

  10. 移植最新版libmemcached到VC++的艰苦历程和经验总结(上)

    零.前言: 该篇博客的Title原计划是“在VC++中调用libmemcached的设计技巧”,可结果却事与原违,原因很简单,移植失败了.尽管结果如此,然而这3天的付出却是非常值得的,原因也很简单,收 ...