Leetcode 104 Maximum Depth of Binary Tree
来源:互联网 发布:文明6 简体中文 mac版 编辑:程序博客网 时间:2024/05/21 19:17
Q:
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.
A:
/**
* 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 maxDepth(TreeNode* root) {
int height = 0,rowCount = 1;
if(root == NULL){
return 0;
}
queue<TreeNode*> queue;
queue.push(root);
while(!queue.empty()){
TreeNode *node = queue.front();
queue.pop();
rowCount --;
if(node->left){
queue.push(node->left);
}
if(node->right){
queue.push(node->right);
}
if(rowCount == 0){
height++;
rowCount = queue.size();
}
}
return height;
}
};
阅读全文
0 0
- Leetcode 104 Maximum Depth of Binary Tree
- LeetCode(104)Maximum Depth of Binary Tree
- LeetCode 104 Maximum Depth of Binary Tree
- LeetCode: Maximum Depth of Binary Tree [104]
- Leetcode(104)Maximum Depth of Binary Tree
- [leetcode 104] Maximum Depth of Binary Tree
- LeetCode 104 Maximum Depth of Binary Tree
- leetCode #104 Maximum Depth of Binary Tree
- LeetCode 104: Maximum Depth of Binary Tree
- LeetCode 104 Maximum Depth of Binary Tree
- leetcode || 104、Maximum Depth of Binary Tree
- LeetCode 104: Maximum Depth of Binary Tree
- leetcode[104]:Maximum Depth of Binary Tree
- Leetcode[104]-Maximum Depth of Binary Tree
- Leetcode---(104)Maximum Depth of Binary Tree
- leetcode-104-Maximum Depth of Binary Tree
- Leetcode #104 Maximum Depth of Binary Tree
- leetcode 104: Maximum Depth of Binary Tree
- Java设计模式(一):工厂模式
- OpenGL预习资料
- @PostConstruct注解
- elasticsearch中的字段类型/mapping参数
- easyui表单多Bean加载与提交
- Leetcode 104 Maximum Depth of Binary Tree
- 主机上连接到 vmware虚拟机的方式
- 架构师之路16年精选50篇
- 使用Jquery的getJSON跨域请求蘑菇街图片资源实现瀑布流
- AOP实现Spring多数据源操作
- 我的github&&Git学习资源
- 82-83_游戏项目_使用继承封装MyFrame作为以后窗口类共同父类
- 基础技术篇 7 —— NB-IoT技术(二)
- Android-APP之桌面宠物