这道题是LeetCode里的第104道题。

给出题目:

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7]

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

返回它的最大深度 3 。

DFS 递归算法,简单的二叉树题。如果看不懂代码,建议学习一下二叉树,这可是基础啊!

给出代码:

递归法:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. //复杂版:
  11. class Solution {
  12. public:
  13. int maxDepth(TreeNode* root) {
  14. if(root==NULL)return 0;
  15. int leftDepth=0;
  16. if(root->left!=NULL)
  17. leftDepth=maxDepth(root->left);
  18. int rightDepth=0;
  19. if(root->right!=NULL)
  20. rightDepth=maxDepth(root->right);
  21. return max(leftDepth,rightDepth)+1;
  22. }
  23. };
  24. //精简版:
  25. class Solution {
  26. public:
  27. int maxDepth(TreeNode* root) {
  28. if(!root)return 0;
  29. return 1+max(maxDepth(root->left),maxDepth(root->right));
  30. }
  31. };

迭代法:

  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int maxDepth(TreeNode* root) {
  13. if(!root)return 0;
  14. queue<TreeNode*> qt;
  15. int max=0;
  16. int preNodeCount=1;
  17. int nodeCount=0;
  18. qt.push(root);
  19. while(qt.size()!=0){
  20. TreeNode* tn=qt.front();
  21. preNodeCount--;
  22. qt.pop();
  23. if(tn->left!=NULL){
  24. qt.push(tn->left);nodeCount++;
  25. }
  26. if(tn->right!=NULL){
  27. qt.push(tn->right);nodeCount++;
  28. }
  29. if(preNodeCount==0){
  30. preNodeCount=nodeCount;
  31. nodeCount=0;
  32. max++;
  33. }
  34. }
  35. return max;
  36. }
  37. };

给出结果:

两个结果都一样,之前做的时候还是 4ms 呢!可能是实例变多了。

给出总结:

能用递归法的就可以试一下用迭代法解决,果不其然,有所收获!一开始使用迭代法使用的是栈,使用栈无论怎么操作,都会产生无限循环的问题,无法得到最终解,因为一般来讲,堆栈适合用来求深度,这道题也是要求深度,可是万万没想到,这道题使用队列来实现迭代,我认为队列适合来求广度,但这题超出我的预料。到时候在找找看有没有用堆栈求解的代码。

哦,想起来了,或许可以用前中后序的代码做文章,改造一下用来求最大深度!!!

【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)的更多相关文章

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

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

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

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

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

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

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

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

  8. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

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

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

随机推荐

  1. Java语言中自动生成随机数

    参考原文:http://zhidao.baidu.com/link?url=nB3BT69wmUAiSPfKGgK5Q7HOFFP9AIE04AthreRd9yYcwKhUQsQRWlMdMhW1Qm ...

  2. 洛谷 P1734 最大约数和

    题目描述 选取和不超过S的若干个不同的正整数,使得所有数的约数(不含它本身)之和最大. 输入输出格式 输入格式: 输入一个正整数S. 输出格式: 输出最大的约数之和. 输入输出样例 输入样例#1: 1 ...

  3. 在Ubuntu16.04安装YouCompleteMe

    作为从事了4年多嵌入式Linux工作的软件工程师,最近决定完全在ubuntu上工作,使用vim进行代码的阅读和编辑,然后尝试去安装vim相关的各种插件.从来没用过代码补全的我,在网上找到了插件omni ...

  4. O2O的十八个细分市场,运营模式如何?

    社区O2O,这个行业也被媒体热炒有三年多时间了,有没有做的还算不错的案例呢?万科.龙湖.恒大.保利.易居中国.彩生活.拉卡拉.顺丰?哪个可以称得上是成功案例?战略变来变去,方向换来换去,基本上都是雷声 ...

  5. 国家气象局提供的天气预报接口(完整Json接口)

    国家气象局提供的天气预报接口主要有三个,分别是:http://www.weather.com.cn/data/sk/101010100.htmlhttp://www.weather.com.cn/da ...

  6. CocoaPods 安装使用

    CocoaPods是一个负责管理iOS项目中第三方开源代码的工具.CocoaPods项目的源码在Github上管理.该项目开始于2011年8月 12日,经过一年多的发展,现在已经超过1000次提交,并 ...

  7. Makefile入门教程

    Makefile介绍 make是一个命令工具,它解释Makefile 中的指令(应该说是规则).在Makefile文件中描述了整个工程所有文件的编译顺序.编译规则.Makefile 有自己的书写格式. ...

  8. nyoj-915—— +-字符串

    http://acm.nyist.net/JudgeOnline/problem.php?pid=915 +-字符串 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 Sh ...

  9. 毛毛虫组【Beta】Scrum Meeting 3

    第三天 日期:2019/6/25 前言 第三次会议: 时间:6月25日 地点:教10-A511 内容:此次会议主要是对项目验收做准备工作. 1.1 今日完成任务情况以及遇到的问题. 今日完成任务情况: ...

  10. spring中常用的注解

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...