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 number of nodes along the shortest path from the root node down to the nearest leaf node.
BFS碰到一个叶子结点就可以了。
class Solution {
public:
int minDepth(TreeNode *root) {
if (root == NULL) return NULL;
queue<TreeNode*> q;
q.push(root);
q.push(NULL);
int h = ;
while (q.size() > 1) {
TreeNode* p = q.front();
q.pop();
if (p == NULL) { h++; q.push(NULL); continue;}
if (p->left == NULL && p->right == NULL) break;
if (p->left) q.push(p->left);
if (p->right) q.push(p->right);
}
return h;
}
};
这里用个NULL指针作哨兵,作为层的结束标志。所有遍历完时,q.size() == 1(q里面只有NULL一个点)。 不过这里因为只要到达叶子结点就会退出,所以不存在死循环的问题。
第三次,bugfree一遍通过。
class Solution {
public:
int minDepth(TreeNode *root) {
if (root == NULL) return ;
vector<vector<TreeNode*> > layers();
int cur = , next = , layer = ;
layers[cur].push_back(root);
while (!layers[cur].empty()) {
layers[next].clear();
for (auto node: layers[cur]) {
if (node->left == NULL && node->right == NULL) return layer;
if (node->left) layers[next].push_back(node->left);
if (node->right) layers[next].push_back(node->right);
}
cur = !cur, next = !next;
layer++;
}
return layer;
}
};
Maximum Depth of Binary Tree
同样是用bfs好记录层数,然后bfs结束返回值就行了。
class Solution {
public:
int maxDepth(TreeNode *root) {
if (root == NULL) return ;
vector<vector<TreeNode*> > layers();
int cur = , next = , layer = ;
layers[cur].push_back(root);
while (!layers[cur].empty()) {
layers[next].clear();
for (auto node: layers[cur]) {
if (node->left) layers[next].push_back(node->left);
if (node->right) layers[next].push_back(node->right);
}
cur = !cur, next = !next;
layer++;
}
return layer;
}
};
Leetcode | Minimum/Maximum Depth of Binary Tree的更多相关文章
- 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 C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- [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 题解]: 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 ...
随机推荐
- Java for LeetCode 199 Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- JavaScript当离开页面时可以进行的操作
当JavaScript离开页面时可以进行的操作 window.onbeforeunload = function() { var email = document.getElementById(&qu ...
- July 22nd, Week 30th Friday, 2016
Love means never having to say you are sorry. 爱就是永远不必说对不起. Love means knowing each other deeply, the ...
- struts2 标签问题----escape="false" 这个属性
1.在编程过程中,会遇到这个动西,escape="false" eg: <s:fielderror escape="false"/>-------& ...
- HTML5中的DOMContentLoaded 和 touchmove
Html5的出现确实解决了一部分页面交互的问题,同时它的一些特性还是没能被我们掌握,今天主要聊聊Html5中的DomcontenLoaded和touchmove事件的属性和使用: DomcontenL ...
- 错误解决error while loading shared libraries: libXXX.so.X: cannot open shared object file: No such file
转自:http://blog.csdn.net/david_xtd/article/details/7625626 前提:ubuntu-debug机器上向SVN提交了pdu-IVT,想在别的普通机器上 ...
- Android开发之日历控件实现
Android开发之日历控件实现:以下都是转载的. 日历控件 日历控件 日历控件 日历控件
- 如何重启Cloudera Manager?
为什么重启: 突然发现ClouderaManager的webui访问不了了…… 我使用netstat看了一下我的webui监听端口,发现尼玛N多CLOSE_WAIT,网上查了一下是Socket关闭有问 ...
- mvc-3模型和数据(1)
MVC和命名空间 var User = function(atts) { this.attribute = atts || {}; } //和具体user相关的方法 User.prototype.de ...
- 回文串+回溯法 URAL 1635 Mnemonics and Palindromes
题目传送门 /* 题意:给出一个长为n的仅由小写英文字母组成的字符串,求它的回文串划分的元素的最小个数,并按顺序输出此划分方案 回文串+回溯:dp[i] 表示前i+1个字符(从0开始)最少需要划分的数 ...