104. Maximum Depth of Binary Tree -- Easy

方法

使用递归

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public: int calDepthRecursion(TreeNode * node) {
if(node == NULL) return 0;
int leftDepth = calDepthRecursion(node->left) + 1;
int rightDepth = calDepthRecursion(node->right) + 1; return std::max(leftDepth, rightDepth);
} int maxDepth(TreeNode* root) {
return calDepthRecursion(root);
}
};
  • Time complexity : we visit each node exactly once, thus the time complexity is \mathcal{O}(N)O(N), where NN is the number of nodes.
  • Space complexity : in the worst case, the tree is completely unbalanced, e.g. each node has only left child node, the recursion call would occur NN times (the height of the tree), therefore the storage to keep the call stack would be \mathcal{O}(N)O(N). But in the best case (the tree is completely balanced), the height of the tree would be \log(N)log(N). Therefore, the space complexity in this case would be \mathcal{O}(\log(N))O(log(N)).

参考

LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告的更多相关文章

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

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

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

  3. (二叉树 BFS DFS) 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 ...

  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

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

  6. leetcode 104 Maximum Depth of Binary Tree ----- java

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

  7. Java [Leetcode 104]Maximum Depth of Binary Tree

    题目描述: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th ...

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

  9. Leetcode 104 Maximum Depth of Binary Tree python

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

随机推荐

  1. Android Studio中使用Git进行代码管理(分支、合并)

    打开Android Studio选择,选择从Git检出代码 也可以从VCS如下点击 去远程仓库复制地址,这里以码云Gitee第三方代码托管为例,类似Github的界面,点击右边复制项目地址 填一下配置 ...

  2. UML之顺序图

    一 定义 顺序图是将交互关系表示为一个二维图.纵向是时间轴(生命线),时间沿竖线向下延伸.横向轴代表了在协作中各独立对象的类元角色.类元角色用生命线表示.当对象存在时,角色用一条虚线表示,当对象的过程 ...

  3. 四、Linux的常用命令

    linux常用命令可以参考这位前辈的:https://www.cnblogs.com/gaojun/p/3359355.html 这篇博文介绍的比较详细!

  4. java的基础数据类型

    Java 里面的数据类型从大的方面分为两类,一是基本数据类型,一是引用类型.基本的JAVA 数据类型层次图如下: Java 中的基本数据类型可分为四种:(1)逻辑型:boolean(2)文本型:cha ...

  5. 本周HTML5的知识点

    html5一般用<meta>标签描述网页的摘要信息.标题标签一共有6个,标题字体加粗<h1>最大,<h6>最小. <p>标签标示内容都在一行显示,结束后 ...

  6. 基于centos6.5安装部署mongdb3.6

    注意:不同的版本的centos,mongdb安装方式不同,请注意版本号!! 基于centos6.5安装部署mongdb3.6 方式有多种,本文介绍使用wget命令来下载获取mongdb,具体命令如下 ...

  7. Linux的.pid文件

    PID全称是Process Identification. PID是进程的代号,每个进程有唯一的PID编号.它是进程运行时系统随机分配的,并不代表专门的进程.在运行时PID是不会改变标识符的,但是你终 ...

  8. Shell bash 数学运算 bc

    1.bc命令可以完成浮点数的运算.其中 scale可以指定保留的小数点位数. 2.举例 例1: 例2:

  9. [dedecms]隐藏栏目不生成静态页面

    重点文件在:/data/cache/inc_catalog_base.inc.php文件,里面存放的是栏目ID 先找到 /dede/sys_cache_up.php(后台更新缓存的地方),找到里面的U ...

  10. shell编程规范

    1 脚本名以.sh结尾,名称尽量见名之意,比如ClearLog.sh Clear_Log.sh clearlog.sh SerRestart.sh Ser_Restart.sh;2 尽量使用UTF-8 ...