leetcode --Minimum Depth of Binary Tree
找最短的到叶节点的长度:
一考虑广度优先搜索(使用队列,不用 recursive)
class Solution {
public:int minDepth(TreeNode* root) {if(!root)return 0;queue<TreeNode*> q;q.push(root);int minheight=0;bool o=false;while(!q.empty()){ minheight++;int count=q.size();cout<<count<<endl;cout<<(q.front())->val<<endl;for(int i=0;i<count;i++){TreeNode* l=q.front();cout<<l->val<<endl;q.pop();if((l->right==NULL)&&(l->left==NULL))return minheight;if(l->right)q.push(l->right);if(l->left)q.push(l->left);}}}};
使用递归形式
class Solution {
public:int minDepth(TreeNode* root) {if (root == NULL) return 0;if (root-> left == NULL && root->right == NULL) return 1;//只有同时没有左节点和右节点才为叶子节点if (root->left == NULL) return 1 + minDepth(root->right);else if (root->right == NULL) return 1 + minDepth(root->left);else return 1 + min (minDepth(root->left),minDepth(root->right)); //有左节点,和右节点,加1 }
};
转载于:https://www.cnblogs.com/fanhaha/p/7240499.html
leetcode --Minimum Depth of Binary Tree相关推荐
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- leetcode - Minimum Depth of Binary Tree
题目:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is th ...
- 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] 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 | Minimum Depth of Binary Tree
题目:给定一个二叉树,找到其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数. 1 /** 2 * Definition for binary tree 3 * public class T ...
- leetcode:Minimum Depth of Binary Tree【Python版】
1.类中递归调用添加self: 2.root为None,返回0 3.root不为None,root左右孩子为None,返回1 4.返回l和r最小深度,l和r初始为极大值: 1 # Definition ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- LeetCode: 111. Minimum Depth of Binary Tree
题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...
最新文章
- 01-iOS获取系统iTunes音乐
- 【Linux系统编程】线程与进程的比较
- 面试最后HR都要问一句有没有什么问题要问我?
- 什么是类加载器?类加载器有哪些
- Exynos4412 Uboot 移植(三)—— Uboot添加自定义命令
- wp config.php mysql_WordPress手动配置wp-config.php文件
- Linux—Ubuntu14.0.5配置JAVA环境
- 面向初学者的带有MVC API的Android 管理表CRUD MSSQL
- CentOS启动报错:Centos kernel panic-not syncing:VFS:Unable to mount root fs on unknown block
- 基于阿里云实现简单的语音识别功能
- 图片云存储(腾讯云 七牛云)
- Linux 安装Oracle10g
- 网络安全-重放攻击(Replay Attack)
- ABAP 资产类BAPI过账 BAPI_ACC_DOCUMENT_POST
- java 输出特殊字符_java原样输出特殊符号
- Linux性能优化 Linux性能工具:CPU top
- node.js中fs.readFile和fs.readFileSync的使用
- Python爬取二级页面(页面分析很重要)
- [Canvas绘图] 第09节 迷图档案
- 阿里巴巴在 Envoy Gateway 的演进历程浅析
热门文章
- 缓冲流、转换流、序列化流
- Oracle 系统改变号SCN详解
- 全国计算机一级考试的练题软件,2017全国计算机一级考试WPS备考练习题
- python霍夫变换检测直线_OpenCV-Python教程(9、使用霍夫变换检测直线)
- centos解压zip命令_2、centos下安装elasticsearch-head
- Android关联启动查询数据库,VIVO X9怎么查看关联启动记录 VIVO X9查看关联启动记录教程...
- oracle+创建序列自增,oracle序列详解和建立自增主键
- jsp mysql utf-8 中文乱码_jsp插入mysql数据库显示中文乱码问题
- linux系统找回数据,Linux系统上面误删的数据找回
- python selenium grid_selenium grid 使用