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. windows下启动mysql服务

    当你无法连接你的mysql数据库时,或者因为某些原因导致与mysql数据库的连接丢失时,可通过以下方式启动mysql服务 1.命令行下启动mysql服务 以管理员身份运行cmd,进入mysql安装目录 ...

  2. 在Windows下通过压缩包方式安装MySQL

    需求:下载MySQL有两种方法,一是下载可执行文件,通过点点点的方式,比较简单没什么技术含量,但是之前通过此方法下载的MySQL与Python进行连接交互的时候总是报1045错误,一直没找到原因,尝试 ...

  3. The 19th Zhejiang University Programming Contest Sponsored by TuSimple (Mirror)

    http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=391 A     Thanks, TuSimple! Time ...

  4. 学习笔记CB009:人工神经网络模型、手写数字识别、多层卷积网络、词向量、word2vec

    人工神经网络,借鉴生物神经网络工作原理数学模型. 由n个输入特征得出与输入特征几乎相同的n个结果,训练隐藏层得到意想不到信息.信息检索领域,模型训练合理排序模型,输入特征,文档质量.文档点击历史.文档 ...

  5. JS制作图片切换

    <!DOCTYPE html> <html> <head> <title>纯JS制作简单的图片切换</title> <meta cha ...

  6. css颜色的设置

    css的颜色设置 1.英文命令颜色 p{color:blue;}RGB颜色 2.与 photoshop 中的 RGB 颜色一致,由 R(red).G(green).B(blue) 三种颜色的比例来配色 ...

  7. zeebe 集成elasticsearch exporter

    zeebe 目前还在一直的开发中,同时一些变动还是挺大的,比如simple monitor 的以前是不需要配置HazelcastExporter的 估计是为了进行集群功能处理,新添加的,以前写的配置基 ...

  8. DevExpress 控件汉化代码和使用方法

    DevExpress 第三方控件汉化的全部代码和使用方法   DevExpress.XtraEditors.Controls  此控件包中包含的控件最多,包括文本框,下拉列表,按钮,等等        ...

  9. Windows 数字化器类输入设备--笔设备分析(1)(原创)

    一.前言: 参考microsoft公司的链接:https://developer.microsoft.com/zh-cn/windows/hardware 将对Windows数字化器类输入设备--笔设 ...

  10. ES6模板字符串【${}配合反单引号一起用】

    先看看JavaScript中两个字符串的效果,就很容易知道模板字符串是个啥东西,其实一点也不新鲜.高级编程中,例如java里面的string.format就是干这个事情,诸如此类. 1. 概念理解 A ...