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 ...
随机推荐
- Cmake在编译osgEarth时遇到的一个错误
CMake Error at src/osgEarthDrivers/CMakeLists.txt:7 (PROJECT): The CMAKE_C_COMPILER: llvm-gcc-4.2 is ...
- el-table中加载图片问题
<el-table-column label="头像" width="100"> <template scope="scope&qu ...
- cocos2d-x 特效集合
本文转载自:http://www.cnblogs.com/linux-ios/archive/2013/04/09/3009292.html bool HelloWorld::init() { /// ...
- jQuery 加法计算 使用+号即强转类型
var value1 = $("#txt1").val(); var value2 = $("#txt2").val(); //数值前添加+号 number加号 ...
- golang micro client 报错500 {"id":"go.micro.client","code":408,"detail":"call timeout: context deadline exceeded","status":"Request Timeout"}
go micro web端连接services时,第一次访问提示500(broken pipe),排查发现客户端请求services时返回 {"id":"go.micro ...
- Struts2整合Spring方法及原理
一. Struts 2框架整合Spring步骤 1. 复制文件.复制struts2-spring-plugin-x-x-x.jar和spring.jar到WEB-INF/lib目录下.其中的x对应 ...
- MySQL 8.0 技术详解
MySQL 8.0 简介 MySQL 5.7 到 8.0,Oracle 官方跳跃了 Major Version 版本号,随之而来的就是在 MySQL 8.0 上做了许多重大更新,在往企业级数据库的路上 ...
- element-ui css 文件加载 失败(https://unpkg.com/element-ui/lib/theme-chalk/index.css,加载失败)
在main.js文件引入 import Vue from 'vue'; import Element from 'element-ui'; import 'element-ui/lib/theme ...
- 原生js实现最简单的瀑布流布局
文章地址 https://www.cnblogs.com/sandraryan/ 瀑布流:瀑布流,又称瀑布流式布局.是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动, ...
- java代码注释:单行//,多行/* */,文档注释/** */
1.单行注释 //: //后到本行结束的所有字符会被编译器忽略; 2.多行注释 /* */: /* */之间的所有字符会被编译器忽略 3.文档注释 /** */: 在/** ...