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. Extjs给gridPanel添加单价双击事件和获取当前行的数据

    有两个小属性,如下 this.on('rowdblclick', this.readContent, this); this.on('cellclick', this.gridCellClick, t ...

  2. 借助magicwindow sdk plugin快速集成sdk

    到目前为止,Android Studio已经是开发原生Android App的主流IDE,它是由Google官方设计并基于JetBrains的IntelliJ IDEA.我们魔窗开发的sdk也是使用此 ...

  3. 解决 jersey javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)

    检查是否Jar冲突 保留一个jersey-server-*.jar

  4. python基础——迭代

    python基础——迭代 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). 在Python中,迭代是通过for .. ...

  5. 谈Web前端安全编码

    最近开发中涉及到有关输出正确的HTML标签这样的问题,正好对字符编码这块儿多看看,之前对这个方面认识的不深,思考的确实不够,如果下次再碰见类似的问题,若再次不少时间去调研的花,就得不偿失了. 就像正则 ...

  6. 二进制日志BINARY LOG清理

    mysql> show master logs; +------------------+-----------+ | Log_name | File_size | +------------- ...

  7. mysql中char,varchar与text类型的区别和选用

    关于char,varchar与text平时没有太在意,一般来说,可能现在大家都是用varchar.但是当要存储的内容比较大时,究竟是选择varchar还是text呢?不知道...... 于是去查阅了一 ...

  8. PHP站内搜索:多关键字、加亮显示

    一.SQL语句中的模糊查找        主要通过LIKE(不区分大小写)关键字实现模糊查找.LIKE条件一般用在指定搜索某字段的时候, 通过"%"或者" _" ...

  9. 【现代程序设计】homework-06

    1) 把程序编译通过, 跑起来. 读懂程序,在你觉得比较难懂的地方加上一些注释,这样大家就能比较容易地了解这些程序在干什么. 把正确的 playPrev(GoMove) 的方法给实现了. 如果大家不会 ...

  10. 【spring 配置文件】spring配置文件的解析

    一.总体结构 二.详解 1.spring <alias >标签 在对bean进行定义时,除了使用id属性来指定名称之外,为了提供多个名称,可以使用alias标签来指定.而所有的这些名称都指 ...