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报Error:(26, 13)-v7:27.错误的解决方法

    1.报错图片 2.上图我画了红圈很明显就提示报错方向就是项目文件:build.gradle 3.解决方法如下 添加此方法到项目构建.gradle文件中: repositories {    maven ...

  2. sql排列组合

    一个表中4条记录,如何查询所有可能组成的结果. 例如: 1  2  3  4 四条记录,最后组成(12),(13),(14),(23),(24),(34) 最后sql如下: SELECT a.name ...

  3. 关于python中的GIL

    什么是GIL锁? GIL是Global Interpreter Lock的缩写,GIL中文可以称为全局解释器锁.提及到GIL,我们要知道它是在实现Python解析器(CPython)时所引入的一个概念 ...

  4. # -*- coding: utf-8 -*-

    -- coding: utf-8 -- import scrapy from jobscrawler_qianchengwuyou.items import JobscrawlerQianchengw ...

  5. URL 通过Get方式传递数组参数

    URL 通过Get方式传递数组参数 方法1: ?id=1&id=2&id=3 后台获取时,只需要reqeust.getParameterValues("id") 获 ...

  6. json格式字符串用Uncaught SyntaxError: Unexpected token ' Uncaught SyntaxError: Unexpected number

    Unexpected number(index)的错误用的json字符串如 var jsonStr = "{1:'北京note备注信息',2:'上海note备注信息',3:'广东note备注 ...

  7. 《多线程操作之生产者消费者》(单生产单消费&多生产多消费)

    说明1:假设有一个放商品的盘子(此盘子只能放下一个商品).生产者每次生产一个商品之后,放到这个盘子里,然后唤醒消费者来消费这个面包.消费者消费完这个商品之后,就唤醒生产者生产下一个商品.前提是,只有盘 ...

  8. Beta冲刺 5

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10129059.html 作业博客:https://edu.cnblogs.com/campus ...

  9. Maven Gradle

    场景:随着项目越来越规范,对构建工具的要求越来越高,我们从Maven转到了Gradle. 转自:http://www.infoq.com/cn/news/2011/04/xxb-maven-6-gra ...

  10. winform devexpress 用法汇总

    废话不多说先上图 1.封装分页控件 qrcodeOnPage1.SearchData(gridControl2, IDataPage, sWhere, "", "tb_o ...