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. VS.net 2013中使用Git建立源代码管理 版本管理

    第一次在VS2013中使用Git,也是第一次使用Git,各种不熟悉.百度各种使用经验,大都不屑于使用VS2013集成的Git,建议下载这个下载那个,我也照学了,确实能实现项目的提交.同步.合并的工作, ...

  2. IT软件开发常用英语词汇

    Aabstract 抽象的abstract base class (ABC)抽象基类abstract class 抽象类abstraction 抽象.抽象物.抽象性access 存取.访问access ...

  3. #一周五# (视频) 手掌四轴Estes 4606,树莓派2和WRTNode,WinHEC 2015深圳

    又到周五,本周<快速创建网站>系列接近尾声,主要部分已经完成,还差最后一篇博主我正在整理.这里给大家分享一些最近看到的有意思的东西. 手掌四轴飞行器 无人机这段时间可是大出风头,DJI或者 ...

  4. JavaScript Patterns 5.2 Declaring Dependencies

    It’s a good idea to declare the modules your code relies on at the top of your function or module. T ...

  5. Flex各类型坐标转换(全局、本地、内容坐标间转换)

    Flex包含3种坐标:全局坐标.本地坐标.内容坐标 全局坐标:stage级别,坐标原点为舞台的左上角,如MouseEvent的stageX.stageY坐标. 本地坐标:组件级别的坐标系,相对坐标,坐 ...

  6. ORACLE 查看有多个执行计划的SQL语句

    在SQL优化过程,有时候需要查看哪些SQL具有多个执行计划(Multiple Executions Plans for the same SQL statement),因为同一个SQL有多个执行计划一 ...

  7. C#语句1:选择语句一(if else )

    语句是指程序命令,都是按照顺序执行的.语句在程序中的执行顺序称为“控制流”或“执行流”. 根据程序对运行时所收到的输入的响应,在程序每次运行时控制流可能有所不同. 注意,语句间的标点符号必须是英文标点 ...

  8. js中的三个编码函数:escape,encodeURI,encodeURIComponent

    1. eacape(): 该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . / .其他所有的字符都会被转义序列替换.其它情况下es ...

  9. 批处理脚本为Mysql重置root密码(重置密码为123456)

    @echo off title mysql ::从注册表找到Mysql的安装路径写入文件mysql.txt reg query HKLM\SYSTEM\ControlSet001\Services\M ...

  10. day 2 常用快捷键

    tab命令或路径补全**,linux里最有用的快捷键,如果tab不到路径或命令,就代表没有这个路径或者命令,还有可能是权限不对. ctrl+c :终止当前任务命令或程序. ctrl+d 退出当前用户环 ...