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的更多相关文章

  1. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

  2. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

  3. [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 ...

  4. (二叉树 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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. CPinyin unicode汉字查找拼音(支持多音字)

    下载代码 --------------------------------------------------------------------------------- 虽然很笨的办法,却非常有效 ...

  2. js: this,call,apply,bind 总结

    对js中的一些基本的很重要的概念做一些总结,对进一步学习js很重. 一.this JavaScript 中的 this 总是指向一个对象,而具体指向那个对象是在运行时基于函数的执行环境动态绑定的,而非 ...

  3. 深入Java单例模式【转载】

    在GoF的23种设计模式中,单例模式是比较简单的一种.然而,有时候越是简单的东西越容易出现问题.下面就单例设计模式详细的探讨一下.   所谓单例模式,简单来说,就是在整个应用中保证只有一个类的实例存在 ...

  4. 彻底禁止QQ更新

    彻底禁止QQ自动更新 彻底禁止QQ自动更新 相信大部分朋友用的QQ都不是腾讯提供的官方原版吧,我本人就用的某精简版本,只保留了自己会用到的个别功能,既省内存,启动也超快,界面更是清爽无比. 但是,这种 ...

  5. Myeclipse 安装svn插件

    安装subclipse,  SVN插件1.从官网下载site-1.8.22.zip文件  访问不了可点我网盘2.从中解压出features与 plugins文件夹,复制到MyEclipse\MyEcl ...

  6. duapp获取mysql用户名密码等等……

    duapp呵呵,又是云!比起新浪,这个免费.下面是:(你懂的) <?php/*数据库名称写自己创建的*/$dbname = 'rjKagJvksJdyUKfyPfjY'; /*从环境变量里取出数 ...

  7. MySQL监控

    http://blog.csdn.net/zreodown/article/details/8158469

  8. duilib WindowImplBase BUG修复 --- 按一次ESC键, 关闭多个窗口

    BUG造成的影响 继承自WindowImplBase的窗口类, 如果没有重写重写ResponseDefaultKeyEvent而由WindowImplBase默认处理的话, 会导致按一次有ESC键, ...

  9. SAE Java相关问题小结

    转自:http://blog.csdn.net/bhq2010/article/details/8580412 Sae中使用的servlet容器是jetty7.4.x 我想在web.xml中配置一个自 ...

  10. 能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项。试图加载格式不正确的程序。

    现象: 能加载文件或程序集“System.Data.SQLite”或它的某一个依赖项.试图加载格式不正确的程序.