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

算法1:dfs递归的求解

 class Solution {
public:
int minDepth(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(root == NULL)return ;
int res = INT_MAX;
dfs(root, , res);
return res;
}
void dfs(TreeNode *root, int depth, int &res)
{
if(root->left == NULL && root->right == NULL && res > depth)
{res = depth; return;}
if(root->left)
dfs(root->left, depth+, res);
if(root->right)
dfs(root->right, depth+, res);
}
};

还有一种更直观的递归解法,分别求左右子树的最小深度,然后返回左右子树的最小深度中较小者+1

 class Solution {
public:
int minDepth(TreeNode *root) {
if(root == NULL)return ;
int minleft = minDepth(root->left);
int minright = minDepth(root->right);
if(minleft == )
return minright + ;
else if(minright == )
return minleft + ;
else return min(minleft, minright) + ;
}
};

算法2:层序遍历二叉树,找到最先遍历到的叶子的层数就是树的最小高度

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//层序遍历,碰到第一个叶子节点就停止,NULL作为每一层节点的分割标志
if(root == NULL)return ;
int res = ;
queue<TreeNode*> Q;
Q.push(root);
Q.push(NULL);
while(Q.empty() == false)
{
TreeNode *p = Q.front();
Q.pop();
if(p != NULL)
{
if(p->left)Q.push(p->left);
if(p->right)Q.push(p->right);
if(p->left == NULL && p->right == NULL)
{
res++;
break;
}
}
else
{
res++;
if(Q.empty() == false)Q.push(NULL);
}
}
return res;
}
};

本文地址


LeetCode:Maximum Depth of Binary Tree

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.

算法1:dfs递归求解

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(root == NULL)return ;
int res = INT_MIN;
dfs(root, , res);
return res;
}
void dfs(TreeNode *root, int depth, int &res)
{
if(root->left == NULL && root->right == NULL && res < depth)
{res = depth; return;}
if(root->left)
dfs(root->left, depth+, res);
if(root->right)
dfs(root->right, depth+, res);
}
};

同上一题

 class Solution {
public:
int maxDepth(TreeNode *root) {
if(root == NULL)return ;
int maxleft = maxDepth(root->left);
int maxright = maxDepth(root->right);
if(maxleft == )
return maxright + ;
else if(maxright == )
return maxleft + ;
else return max(maxleft, maxright) + ;
}
};

算法2:层序遍历,树的总层数就是树的最大高度

 /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//层序遍历计算树的层数即可,NULL作为每一层节点的分割标志
if(root == NULL)return ;
int res = ;
queue<TreeNode*> Q;
Q.push(root);
Q.push(NULL);
while(Q.empty() == false)
{
TreeNode *p = Q.front();
Q.pop();
if(p != NULL)
{
if(p->left)Q.push(p->left);
if(p->right)Q.push(p->right);
}
else
{
res++;
if(Q.empty() == false)Q.push(NULL);
}
}
return res;
}
};

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3440059.html

LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree的更多相关文章

  1. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  2. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  3. 【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree

    解法一:递归 int maxDepth(TreeNode* root) { if (root == NULL) ; int max_left_Depth = maxDepth(root->lef ...

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

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

  6. 2016.6.26——Maximum Depth of Binary Tree

    Maximum Depth of Binary Tree 本题收获 1.树时使用递归 2.注意边界条件时输出的值,仔细阅读题意,若是面试时,问清边界条件. 题目: Given a binary tre ...

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

  8. [LeetCode#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree

    The problem 1: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes ...

  9. [LeetCode] Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

随机推荐

  1. db2+python+sqlchemy环境的搭建

    记录了通过sqlalchemy 管理db2数据库的环境搭建 1.db2数据库安装配置 利用winscp复制iso文件到/mnt/IBM_db2 目录下 IBM_db2为自己创建 重命名 mv IBM\ ...

  2. CMPP3.0实现物联网卡通讯

    当下物联网发展迅猛,物联网卡可以接受短信指令,实现千里之外尽可掌控.本人做过一个这类项目,把相关经验记录下来,分享给需要的人. 物联网卡通讯其实跟电话卡一样,可以使用CMPP协议.不过由于物联网卡位数 ...

  3. cdh集群数据恢复

    CDH 数据库 磁盘坏了  所有集群配置 都没了    而且 还没备份  ....    元数据 还在  cdh  软件配置 和  安装软件 不能用了 下载 apache hadoop   重新配置  ...

  4. RabbitMQ入门教程——安装及配置

    RabbitMQ是一个消息代理,一个消息系统的媒介,提供了一个通用的消息发送及接收平台,并且能够保障消息传输过程中的安全.使用erlang语言开发,开源,在易用性.扩展性.高可用性等方面表现不俗 技术 ...

  5. mysql错误一例:ERROR 1030 (HY000): Got error 28 from storage engine

    在使用mysqldump导出一份建库脚本是,发生了下面的错误: 当执行 desc table_name; 时也报错: tag为表名,show index from tag;倒是可以执行. 其实真正的错 ...

  6. 烂泥:使KVM显示VM的IP地址及主机名

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. KVM虚拟化学习已经有一段时间了,现在虚拟化软件比较多,对比了下目前使用比较多的VMware Vsphere.发现在不进入VM系统的情况下,Vspher ...

  7. iOS打包Framework真机和模拟器兼容合并版本 - 详细攻略步骤

    打包Framework,测试时: 1.用模拟器打包,测试时只能跑在模拟器 2.用真机打包,测试时只能跑在真机 那么怎么做到一个版本兼容以上两种场景呢? 解决如下: 1.打开终端 2.输入   lipo ...

  8. net-snmp添加自定义MIB

    我所知道的添加自定义MIB的方法有三种   1.静态加载,将生成的.c和.h文件加入到相应的位置,重新编译snmp库,优点是不需要修改配置文件,缺点是每次添加都得重新编译: 2.动态加载,将生成的.c ...

  9. GNU C 内联汇编介绍

    GNU C 内联汇编介绍 简介 1.很早之前就听说 C 语言能够直接内嵌汇编指令.但是之前始终没有去详细了解过.最近由于某种需求,看到了相关的 C 语言代码.也就自然去简单的学习了一下如何在 C 代码 ...

  10. iTOP-4412嵌入式开发板ioremap控制GPIO寄存器

    转自迅为电子技术论坛:http://bbs.topeetboard.com GPIO 的寄存器通过 ioremap函数转换之后,可以通过直接控制虚拟地址来控制物理地址(寄存器的实际地址),这样就实现 ...