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.

Note: A leaf is a node with no children.

Example:

Given binary tree [,,,null,null,,],

   / \

    /  \

return its depth = 

方法一:采用递归方法:(C++)

①如果一棵树只有一个结点,它的深度为1。

②如果根结点只有左子树而没有右子树,那么树的深度应该是其左子树的深度加1;同样如果根结点只有右子树而没有左子树,那么树的深度应该是其右子树的深度加1。

③如果既有右子树又有左子树,那该树的深度就是其左、右子树深度的较大值再加1。

 int maxDepth(TreeNode* root) {
if(!root)
return ;
int nleft=maxDepth(root->left);
int nright=maxDepth(root->right);
return nleft>nright?nleft+:nright+;
}

Java:

 public int maxDepth(TreeNode root) {
if(root==null)
return 0;
int nleft=maxDepth(root.left);
int nright=maxDepth(root.right);
return nleft>nright?nleft+1:nright+1;
}

方法二:采用迭代方法:利用队列先进先出的特性,将每一层的结点弹出后,再将其左右子树压进队列,直至该层的结点全部弹出(C++)

 int maxDepth(TreeNode* root) {
if(!root)
return ;
int res=;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
res++;
for(int i=q.size();i>;i--){
TreeNode* t=q.front();
q.pop();
if(t->left)
q.push(t->left);
if(t->right)
q.push(t->right);
}
}
return res;
}

Java:

 public int maxDepth(TreeNode root) {
if(root==null)
return 0;
Queue<TreeNode> m=new LinkedList<>();
m.offer(root);
int res=0;
while(!m.isEmpty()){
res++;
for(int i=m.size();i>0;i--){
TreeNode t=m.poll();
if(t.left!=null)
m.offer(t.left);
if(t.right!=null)
m.offer(t.right);
}
}
return res;
}

LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java的更多相关文章

  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 ☆(二叉树的最大深度)

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

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

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

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

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

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

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

  6. 104 Maximum Depth of Binary Tree 二叉树的最大深度

    给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7],    3   / \  9  20    /  ...

  7. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

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

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. 网络编程三 Socket

    1.根据netstat端口的找到进程号---->根据进程号找到进程名称-------->终止进程 1) netstat    最后一列是5432 C:\Users\Administrato ...

  2. C++ 中用cin方式获取输入的几种常用方式

    一.前言 在C++程序的编写过程中,可能会经常用到cin方式用来捕获输入设备的输入信息.细分的话,主要的方式有:cin>>.cin.get.cin.getline.在借助键盘等字符输入设备 ...

  3. 关于定时器setTimeout()方法的实践--巧解bug

    _使用开发环境:UAP:_ _框架:JQuery.MX:_ 最近的开发的页面中,有一处需要在提交的 datagrid里启用行编辑,就会发生奇怪的bug,编辑过程中如图所示不移开焦点直接点保存,那么已输 ...

  4. Centos7快速部署saltstack

    saltstack是一个和ansible差不多的自动化运维工具,可以用来批量管理大量主机 OS:centos7.3 server:172.16.13.159 client: 172.16.13.156 ...

  5. day-12函数对象

    函数默认值的细节 如果函数的默认参数的默认值为变量,在所属函数定义阶段一执行就被确定为当时变量存放的值,后面变化不会再变化 a = 100 def fn(num=a): a = 200 fn() 三元 ...

  6. 深入理解JavaScript事件循环机制

    前言 众所周知,JavaScript 是一门单线程语言,虽然在 html5 中提出了 Web-Worker ,但这并未改变 JavaScript 是单线程这一核心.可看HTML规范中的这段话: To ...

  7. OpenJ_Bailian 4148 生理周期

    生理周期 OpenJ_Bailian - 4148 Time limit1000 ms Memory limit65536 kB OS Linux SourceEast Central North A ...

  8. Azure CosmosDB (11) MongoDB概念

    <Windows Azure Platform 系列文章目录> Azure Cosmos DB兼容MongoDB的API,下表将帮助我们更容易理解MongoDB中的一些概念: SQL概念 ...

  9. mybatis的逆向工程和中文注解

    由于MyBatis Generator自带了生成注释的功能,但是,是英文的而且生成的根本无法理解,所以可以通过,修改他的源码来实现生成中文的注释,具体方式有以下几种: 1) 自定义CommentGen ...

  10. MySQL面试题中:主从同步的原理

    主从同步的原理:1.主库上面有一个IO线程,从库上有一个IO线程和一个SQL线程,从库中的IO线程负责从主库读取binlog,并写入从库的中继日志:SQL线程负责读取并执行中继日志中的binlog,转 ...