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. json之dump和dumps,load和loads

    import json#反序列化f = open('test', 'rb')data = json.load(f) #相当于下句# data = json.loads(f.read()) #序列化f ...

  2. jquery添加节点时能有点击事件

    <script>            var n=0;            $(".dj").on('click',function(){              ...

  3. Python基础-Python的三元运算符和lambda表达式

    1. Python的三元表达式: 现在大部分高级语言都支持 “?”这个三元运算符,它对应的表达式如下:condition ? value if true:value if else 但是 Python ...

  4. python笔记20-装饰器、作用域

    函数的作用域是就近原则,从里往外找,如果自己函数里有,就拿过来如果自己的函数里面没有的话,就去它父级函数里面找,父亲用不了儿子的,儿子可以用父亲的函数只有被调用才会执行# name = 'python ...

  5. CSS1 !important

    CSS1 !important 提升指定样式规则的应用优先权 ie6并不支持.还是会被后面的样式覆盖

  6. (2018 Multi-University Training Contest 3)Problem L. Visual Cube

      //题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6330//题目大意:按照一定格式画出一个 a×b×c 的长方体.  #include <b ...

  7. java基础知识—循环结构

    1.while 循环 语法: while(循环操作){ 循环操作: } 特点:先判断,再执行:2. == : 用于数字比较 比较的是地址 equals: 用于字符串比较 比较的是字符 3.do-whi ...

  8. python之常用模块学习

    1.模块调用 import module from module import xx from module.xx.xx import xx as rename from module.xx.xx i ...

  9. 小程序 onReachBottom 事件快速滑动时不触发的bug

    一般在列表页面 会先加载一定数量的数据 触发上拉加载这个动作的时候再陆续加载数据 假如上拉一次加载10条数据 在小程序中 你快速滑动页面触发加载这个事件的话 你会发现小程序卡着不动了 刚开始以为数据加 ...

  10. APICloud-数据存储

    APICloud 共有四种数据存储 1.file:目录操作,文件操作 文件存储方式,用于图片.文档的上传.下载.删除.管理. 2.db:本地 sqlite 数据库 用于离散数据的存储. 3.loacl ...