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 ...
随机推荐
- pytorch 多GPU训练总结(DataParallel的使用)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/weixin_40087578/artic ...
- Mybatis表关联多对多
创建表 创建表对应的 JavaBean 对象 package com.tanlei.newer.model; import java.util.List; /** * @author:Mr.Tan * ...
- Ubuntu matplotlib显示中文乱码的解决方法
https://blog.csdn.net/huuinn/article/details/78968966
- laravel中如何实现验证码验证及使用
开发环境: laravel5.5 php7.1.11 mysql 验证码 是防止恶意破解密码.刷票.论坛灌水.刷页的手段.验证码有 多种类型. 现在我给大家实现如何使用图片验证码,其原理是让用户输入一 ...
- 自定义View系列教程06--详解View的Touch事件处理
深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...
- 10-1 body标签里面相关的标签(列表,表单,表格)
一 列表标签<ul>,<ol>,<dl> <!DOCTYPE html> <html lang="en"> <he ...
- Android中使用Apache common ftp进行下载文件
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/birdsaction/article/details/36379201 在Android使用ftp下 ...
- php parse_url linux 解析问题
耕毅 解析url函数parse_url() (PHP 4, PHP 5, PHP 7) parse_url — 解析 URL,返回其组成部分 mixed parse_url ( string $url ...
- @noi.ac - 441@ 你天天努力
目录 @description@ @solution@ @accepted code@ @details@ @description@ 你天天努力,还是比不上小牛,因为小牛在家中套路.于是你决定去拜访 ...
- ORACLE内部操作
当执行查询时,ORACLE采用了内部的操作. 下表显示了几种重要的内部操作. ORACLE Clause 内部操作 ORDER BY SORT ORDER BY UNION UNION-ALL MIN ...