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. Java for LeetCode 187 Repeated DNA Sequences

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  2. Enum:Hopscotch(POJ 3050)

    跳格子 题目大意:牛像我们一样跳格子,一个5*5的方格,方格有数字,给牛跳5次,可以组成一个6个数字组合字符串,请问能组合多少个字符串? 题目规模很小,暴力枚举,然后用map这个玩具来检测存不存在就可 ...

  3. hdu 1160 FatMouse's Speed 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1160 题目意思:给出一堆老鼠,假设有 n 只(输入n条信息后Ctrl+Z).每只老鼠有对应的weigh ...

  4. Codeforces 390A( 模拟题)

    Inna and Alarm Clock Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64 ...

  5. [Java基础] SequenceInputStream输入合并流

    转载: http://blog.csdn.net/xuefeng1009/article/details/6955707 public SequenceInputStream(Enumeration& ...

  6. opencv学习笔记(三)基本数据类型

    opencv学习笔记(三)基本数据类型 类:DataType 将C++数据类型转换为对应的opencv数据类型 OpenCV原始数据类型的特征模版.OpenCV的原始数据类型包括unsigned ch ...

  7. 说说GET和POST方法的区别

    完全来自博客园的一篇文章,GET和POST有什么区别?说的非常有道理,学习了. 错误理解 反驳 GET使用URL或Cookie传参.而POST将数据放在BODY中. GET和POST与数据如何传递没有 ...

  8. Struts2中配置默认Action

    1.当访问的Action不存在时,页面会显示错误信息,可以通过配置默认Action处理用户异常的操作:2.配置方法:    在struts.xml文件中的<package>下添加如下内容: ...

  9. SPI的通信试验 --verilog (从机-全双工)

    SPI的 有关知识参考FPGA作为主机的通信实验. 本实验中FPGA作为从机通过SPI与MCU等通信的试验,可以在时钟上升沿接收数据并且在时钟下降沿发送数据,模仿全双工模式.接收的 数据作为地址,通过 ...

  10. php 面向对象之继承、多态和静态方法

    <?php //继承:子类可以继承父类的一切 //特点:单继承 //父类 class Ren { public $name; public $sex; public $yuyan; functi ...