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. 小菜鸟从0基础开始学Linux系统

    随着当今信息时代的迅速发展,Linux凭借其诸多优势从操作系统中脱颖而出,受到越来越多电脑用户的青睐.Linux是一个集安全.稳定.自由等众多优点于一身的操作系统,不可思议的是这么好的系统还是免费的! ...

  2. 关于Servlet的一些归纳(1)

    1.servlet接口 含5个抽象放法 void init(ServletConfig config) throws ServletException //初始化Servlet void servic ...

  3. Git bash 配置多个远端仓库

    $ cat .ssh/config #aliyeye Host aliyeye.com.cn HostName aliyeye.com.cn PreferredAuthentications publ ...

  4. EFCore Lazy Loading + Inheritance = 干净的数据表 (二) 【献给处女座的DB First程序猿】

    前言 本篇是上一篇EFCore Lazy Loading + Inheritance = 干净的数据表 (一) [献给处女座的DB First程序猿] 前菜 的续篇.这一篇才是真的为处女座的DB Fi ...

  5. 关于Opengl投影矩阵

    读 http://www.songho.ca/opengl/gl_projectionmatrix.html 0.投影矩阵的功能: 将眼睛空间中的坐标点 [图A的视椎体]     映射到     一个 ...

  6. python异常(概念、捕获、传递、抛出)

    异常 目标 异常的概念 捕获异常 异常的传递 抛出异常 01. 异常的概念 程序在运行时,如果 Python 解释器 遇到 到一个错误,会停止程序的执行,并且提示一些错误信息,这就是 异常 程序停止执 ...

  7. Vue语法学习第四课(1)——组件简单示例

    在 Vue 里,一个组件本质上是一个拥有预定义选项的一个 Vue 实例. 设法将应用分割成了两个更小的单元.子单元通过 prop 接口与父单元进行了良好的解耦. <div id="ap ...

  8. 自学python之路(day6)

    一 函数的定义与调用 现在需要一个程序来实现len()的功能. 计算字符串 s 长度 s='好好学习' #函数的定义def my_len(): i = for k in s: i += print(i ...

  9. python笔记3——字符串的操作

    #Author:Wildwolf name="my name is wildwolf ," print(name.capitalize()) #首字母大写 print(name.c ...

  10. MySQL最基本的概念梳理

    本文根据<MySQL必知必会>(Ben Forta著,2009)整理,基于MySQL4.1-5,可作为深入研究MySQL之前的漱口篇.(基本语句.正则表达式.联结.全文本搜索.增删改查.存 ...