leetcode_question_104 Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
DFS:
int maxDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root == NULL) return 0;
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return left > right ? left + 1 : right +1;
}
BFS:
int maxDepth(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root == NULL) return 0;
queue<TreeNode*> que;
que.push(root);
int count = 1;
int depth = 0;
while(!que.empty())
{
TreeNode* tmp = que.front();
que.pop();
count--;
if(tmp->left)
que.push(tmp->left);
if(tmp->right)
que.push(tmp->right);
if(count == 0)
{
depth++;
count=que.size();
}
}
return depth;
}
leetcode_question_104 Maximum Depth of Binary Tree的更多相关文章
- [LintCode] 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][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 104. Maximum Depth of Binary Tree(C++)
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- 【LeetCode练习题】Maximum Depth of Binary Tree
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
随机推荐
- 【转】如何设置无线路由器的信道以获得最佳WIFI体验?
原文网址:http://jingyan.baidu.com/album/f25ef2546e28e4482c1b8225.html 现在随着移动互联网的发展,移动终端的普及,WIFI越来越必不可少,所 ...
- cc150 Chapter 2 | Linked Lists 2.5 add two integer LinkedList, return LinkedList as a sum
2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ...
- Conquering Keokradong && Get the Containers(二分)
Conquering Keokradong Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- 使用disqus搭建comment时一件非常二的事
近期在github 上面搭建自己的博客,搭建comment部分的时候出现了一个问题:配置都配置好了,可是comment就是不成功.昨天为这个问题折腾了了半晚上没找出原因,今天晚上我突然发现一个地方设置 ...
- 【点击模型学习笔记】Predicting Clicks_Estimating the Click-Through Rate for New Ads_MS_www2007
概要: 微软研究院的人写的文章,提出用逻辑回归来解决ctr预估问题,是以后ctr的经典解决方式,经典文章. 详细内容: 名词: CPC -- cost per click CTR -- click t ...
- Android,使用Json发送数据中,使用的Java转义字符 KanKan原创
kankan原创 与php后台发送数据的时候.要求用到这样的格式. private void sendJson(){ //初始化自己定义的handler CashHandler handler = n ...
- Android应用程序组件Content Provider应用实例
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6950440 文简要介绍了Android应用程序 ...
- [Python] 发送email的几种方式
python发送email还是比較简单的,能够通过登录邮件服务来发送,linux下也能够使用调用sendmail命令来发送,还能够使用本地或者是远程的smtp服务来发送邮件,无论是单个,群发,还是抄送 ...
- with ffmpeg to encode video for live streaming and for recording to files for on-demand playback
We've been doing some experimentation with ffmpeg to encode video for live streaming and for recordi ...
- Java Junit4测试功能
作为一个java新手,有些东西有必要记下来,以便以后方便查看. 为了让自己有养成良好的习惯,新建一个测试的文件夹:test在项目上右击→New→Folder.新建一个test文件夹. 接下来要在你需要 ...