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. javaWeb-Servlet工作原理

    1.客户发出请求—>Web 服务器转发到Web容器Tomcat: 2.Tomcat主线程对转发来用户的请求做出响应创建两个对象:HttpServletRequest和HttpServletRes ...

  2. java面试题03

    1.一个”.java”源文件中是否可以包含多个类(不是内部类)?有什么限制?一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制?这个是可以的,一个“.java” ...

  3. c——简单排序

    1.交换排序 a.冒泡排序 #include <stdio.h> int main() { , , , , }, i, j, t; ; i>=; i--) { ; j<i; j ...

  4. SQL Server信息偏差影响表联结方式统计

    SQL Server统计信息偏差影响表联结方式案例浅析   我们知道数据库中的统计信息的准确性是非常重要的.它会影响执行计划.一直想写一篇关于统计信息影响执行计划的相关博客,但是都卡在如何构造一个合适 ...

  5. P2257 莫比乌斯+整除分块

    #include<bits/stdc++.h> #define ll long long using namespace std; ; int vis[maxn]; int mu[maxn ...

  6. JavaScript作用域(第七天)

    我们都知道js代码是由自上而下的执行,但我们来看看下面的代码: test(); function test(){ console.log("hello world"); }; 如果 ...

  7. Comedi的安装

    1.comedi的安装:(需要注意的是comedi和comedilib安装顺序随意)    1.comedi与大多数2.2,2.4和2.6Linux内核配合使用,不支持2.6-2.6.6的内核,对2. ...

  8. 使用VMWare虚拟机打开MFC报错:不支持16位系统

    可能这个问题的比较小众,但还是提供一下自己的思路. 笔者使用的是VMWare Fusion11的版本,采用windows7sp1的虚拟机. 在打开Mac系统共享过来的VC++的MFC文件运行时报错:不 ...

  9. Day 16 模块和包的导入

    包的认识 包通过文件夹来管理一些列功能相近的模块 包:一系列模块的集合体 重点:包中一定有一个专门来管理包中所有模块的文件 包名:存放一系列模块的文件夹名字 包名(包对象)存放的是管理模块的那个文件地 ...

  10. SparkStream:4)foreachRDD详解

    转载自:http://blog.csdn.net/jiangpeng59/article/details/53318761 foreachRDD通常用来把SparkStream运行得到的结果保存到外部 ...