LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
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++ 解题报告的更多相关文章
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- [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 ...
- (二叉树 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Mvc请求的生命周期
ASP.NET Core : Mvc请求的生命周期 translation from http://www.techbloginterview.com/asp-net-core-the-mvc-req ...
- HDU1237
/************************************************************** 作者:陈新 邮箱:cx2pirate@gmail.com 用途:hdu1 ...
- vue 特点
1.双向绑定 v-model 2.组件化 页面扩展 单文件组件 js css html 都在一个文件中 好处:前端组件化的突破性设计 scoped限制css的渲染,防止污染 lang 定义预处理器 ...
- Java利用cors实现跨域请求
由于ajax本身实际上是通过XMLHttpRequest对象来进行数据的交互,而浏览器出于安全考虑,不允许js代码进行跨域操作,所以会警告 网站开发,在某些情况下需要用到跨域. 什么是跨域? 跨域,指 ...
- 介绍一下Spring Cloud简介
Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线).分布式系统的协调导致了样板模式, 使用Spring Clo ...
- Exploit-Exercises nebule 旅行日志(四)
接着上次的路程继续在ubuntu下对漏洞的探索练习,这次是level03了 先看下level03的问题描述: 精炼下问题,在/home/flag03的目录下有个crontab的文件是每分钟都在执行 这 ...
- VGG网络结构
这个结构其实不难,但是它里面训练的一些东西我还没有搞清楚,打算把昨天写的代码传上来,方便日后来看,发现了一个很有意思的库叫TF-slim打算哪天看看有没有好用的东西 from datetime imp ...
- elasticsearch 的内存JVM和GC相关问题
JVM对ElasticSearch集群的稳定性有很大的影响. Java是一个垃圾收集语言,意思是这个程序不会手动管理分配和释放内存.程序员只需要编写代码,jvm管理根据需要管理分配内存的处理,然后在不 ...
- tp5 删除服务器文件
public function test(){ //ROOT_PATH . 'public' . DS . 'uploads' $filename = ROOT_PATH . 'public' . D ...
- Scrapy的使用
建立好项目以后,在项目文件内scrapy会搭好框架,我们只需要按照框架设置. 先定义Item 它是保存爬取到的数据的容器,其使用方法和python的字典类似,并且提供了额外保护机制来避免拼写错误 ...