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 ...
随机推荐
- 软件151 王楚博 struts
一.下载Struts 建立web项目,给项目添加外部引用包(project-properties-Java Build Path-Add External Jars...).添加的包有:commons ...
- JavaEE第六周
Applet简介 Java Applet简介 最近要使用worldwind java sdk做Applet开发,看了些Applet的资料,为了防止忘记,记录如下: applet是通过<apple ...
- shell脚本的多线程
shell脚本的多线程 #!/bin/bash ###这是个多线程脚本!!!! ..} do { .$i >/dev/null ];then echo "192.168.2.$i 存活 ...
- CentOS7基本配置一
CentOS7基本配置一 安装VMwareTools 1.点击重新安装VM-tool, 继而找到压缩文件VMwareTools-10.2.0...tar.gz,复制到桌面下,解压这么压缩文件到桌面下 ...
- C# WebAPI分页实现分享
第一次分享代码,不足或不对之处请指正.. 需求:微信端传递不同的参数调用WebAPI进行分页查询菜谱计划点评结果 思路:基于视图来查询,根据传递的不同参数拼接分页查询Sql来查询. 分页的sql如下 ...
- Hyperledger Fabric-CA学习
Hyperleder Fabric系统架构核心逻辑包括MemberShip.Blockchain和Chaincode 其中上述3个核心逻辑中,Membership服务用来管理节点身份.隐私.confi ...
- Nginx原理
原理机制 Nginx采用多进程(每个worker进程只对应一个线程)和I/O多路复用机制,实现并发的事件驱动处理: 多路复用即通过一种机制监视多个文件描述符,一旦文件描述符就绪(读写就绪),就可通知程 ...
- 使用Xshell配置外网访问端口
- DeepCTR专题:DeepFM论文学习和实现及感悟
论文地址:https://arxiv.org/pdf/1703.04247.pdf CTR预估我们知道在比较多的应用场景下都有使用.如:搜索排序.推荐系统等都有广泛的应用.并且CTR具有极其重要的 地 ...
- 2018-2019-2 网络对抗技术 20165326 Exp3 免杀原理与实践
免杀原理与实践 目录 知识点问答 实践内容 遇到的问题 心得体会 知识点 meterpreter免杀 基础问题回答 杀软是如何检测出恶意代码的? 特征码(基于签名):模式匹配,比对特征码库 启发式:通 ...