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. arp嗅探(windows)

    本次实验环境:windows本次实验工具:cain汉化版1.点击配置,嗅探器里选一个适配器,点击确定. 2.点击 3.扫描mac地址 4.点击ARP->嗅探器->添加到列表5.点击开始嗅探 ...

  2. java第八次课堂笔记

  3. JS案例六_1:添加城市

    使用的相关知识点:对子节点的添加:document.appendClild() 文本节点的创建:document.createTextNode() 元素节点的创建:document.createEle ...

  4. tp备份数据

    <?php namespace Chenlin2103\Controller; class BaksqlController extends MainController{ public $co ...

  5. C# 向程序新建的窗体中添加控件,控件需要先实例化,然后用controls.add添加到新的窗体中去

    C# 向程序新建的窗体中添加控件,控件需要先实例化,然后用controls.add添加到新的窗体中去 Form settingForm = new Form(); setForm deviceSet ...

  6. web语言发展史

    引用自CSDN,地址:https://blog.csdn.net/moshenglv/article/details/51590830 提到Web,不得不提一个词就是“互联网”.Web是World W ...

  7. U盘安装kali中CDROM问题解决

    1,采用ultraISO 制作Kali的安装盘,无法安装.U盘不能被识别. 2,改用win32diskimager,能识别了,但期间出现CDROM找不到. 3,采用网上方式,ALT+ F2,ls /d ...

  8. 1011. World Cup Betting (20)

    生词(在文中的意思) tie 平局 lay a bet 打赌 putting their money where their mouths were 把他们的钱用在刀刃上 manner of 的方式 ...

  9. Ruby中puts,print,p的区别

    如果字符串的行尾没有包含换行符,puts就会添加一个,但print不会: print会精确打印内容并让光标留在末尾(在某些系统平台,在程序输出的末尾会自动换行): p会输出一个审查字符串,它通常会包含 ...

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

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