LeetCode111_求二叉树最小深度(二叉树问题)
题目:
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:利用递归遍历,求最小深度
//递归遍历求最小深度
class Solution {
public:
int run(TreeNode *root) {
if(root ==NULL)
return ;
int l = run(root->left);
int r = run(root->right);
if(l== || r==)
return l+r+;
return min(l, r) + ;
}
};
思路2:利用队列采用层序遍历,一旦找到一个叶节点,它肯定是最短的。
参考链接:
二叉树的层序遍历算法:https://blog.csdn.net/qq_29542611/article/details/79372678
class Solution {
public:
int minDepth(TreeNode* root) {
if(root == NULL) return ; //处理边界情况,空树
queue<TreeNode*> q; //辅助数据结构-队列,用于层序遍历
TreeNode* now = root; //当前考察的节点,初始值为root
TreeNode* last = root; //所在层的最后一个节点,初始值为root
int level = ; //返回值最小深度,初始值为1
int size = ; //用于确定是否到达叶子节点
q.push(root); //根压入队列
while(!q.empty())
{
now = q.front();
q.pop();
size = q.size(); //记录队列长度
//左右孩子非空,压入队列
if(now->left != NULL)
q.push(now->left);
if(now->right != NULL)
q.push(now->right);
//如果没有左右孩子入队,长度没有变化,说明这个节点是叶子节点,找到叶子节点,退出循环
if(size == q.size())
break;
//检查是否到达当前层最后一个节点,
if(last == now)
{
level++;
if(!q.empty())
//last指向下一层的最后一个节点
last = q.back();
}
}
return level;
}
};
C++队列Queue类成员函数如下:
back()返回最后一个元素
empty()如果队列空则返回真
front()返回第一个元素
pop()删除第一个元素
push()在末尾加入一个元素
size()返回队列中元素的个数
总结:
二叉树操作主要还是利用尾递归或者循环遍历这两种思路,进而涉及DFS(主要利用递归或者栈实现)或者BFS(主要利用队列实现)。剩下的只需要按照这些思路即可。
广度优先BFS,深度优先DFS;
LeetCode111_求二叉树最小深度(二叉树问题)的更多相关文章
- 【JAVA】【leetcode】【查找二叉树最小深度】
题目: minimum-depth-of-binary-tree 要求:Given a binary tree, find its minimum depth.The minimum depth i ...
- LinCode 刷题 之二叉树最小深度
http://www.lintcode.com/zh-cn/problem/minimum-depth-of-binary-tree/ 题目描述信息 /** * Definition of Tree ...
- [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 ...
- leetcode-111. 二叉树最小深度 · Tree + 递归
题面 找出二叉树的最小深度(从根节点到某个叶子节点路径上的节点个数最小). 算法 算法参照二叉树的最大深度,这里需要注意的是当某节点的左右孩子都存在时,就返回左右子树的最小深度:如果不都存在,就需要返 ...
- 从"按层次输出二叉树"到"求解二叉树深度"的总结
本文是在学习中的总结,欢迎转载但请注明出处:http://write.blog.csdn.net/postedit/41964669 最近在刷LeetCode上的算法题,发现好多题目的解题思路大体是一 ...
- 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度
求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- LeetCode OJ:Minimum Depth of Binary Tree(二叉树的最小深度)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
随机推荐
- 原生js复习1.0
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 如何用django框架完整的写一个项目
实现目标及功能,增删改,并且实现搜索,分页,日期插件,删除提示,以及批量导入等功能 软件版本: python3.5 django1.11 一 用pycharm创建一个项目,名字自定义 二 编辑url ...
- Python基础:12函数细节
一:返回值 当没有显式地返回元素时,Python 会返回一个None.如果函数返回多个对象,python 把他们聚集起来并以一个元组返回. 二:创建函数 1:强烈推荐,在函数体之前,编写函数的文档字符 ...
- linux服务器时间更新
yum install ntpdate ntpdate ntp1.aliyun.com(阿里云服务器时间)
- python selenium 测试配置信息(URL和浏览器)
config.ini # this is config file, only store browser type and server URL [browserType] #browserName ...
- redis 清除缓存
- js常见运算符
博客地址 :https://www.cnblogs.com/sandraryan/
- c++第一次的个人作业
循环结构是c++中重要的结构用以实现代码的反复使用 三种不同的循环结构以及嵌套是必须认识与能够使用的 三种循环结构分别是: 在for循环中可以在for后的括号内加入初始条件,循环条件与参数变化.使得整 ...
- pytorch 优化器调参
torch.optim 如何使用optimizer 构建 为每个参数单独设置选项 进行单次优化 optimizer.step() optimizer.step(closure) 算法 如何调整学习率 ...
- poj 3334 Connected Gheeves (Geometry + BInary Search)
3334 -- Connected Gheeves 题意是,给出两个尖形的相连的容器,要求向其中灌水.它们具有日常的物理属性,例如两个容器中水平面高度相同以及水高于容器顶部的时候就会溢出.开始的时候打 ...