LeetCode OJ——Minimum Depth of Binary Tree
http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/
贡献了一次runtime error,因为如果输入为{}即空的时候,出现了crash。其实这种情况也处理了,但是顺序不对,如下:
if(root->left == NULL && root->right == NULL)
return 1; if(root==NULL )
return 0;
如果输入是空的话,会对空->left访问left,但这是错误的。所以,应该先判断是否为空。即把两个if换个顺序就好了。
广搜,一直到有个点是叶子节点,然后返回深度。经过两道类似的题目,word ladder,求最小深度的时候,优先使用广搜。
#include <iostream>
#include <queue> using namespace std; 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.
if(root==NULL )
return ;
if(root->left == NULL && root->right == NULL)
return ; queue<pair<TreeNode* ,int > > nodeQueue;
nodeQueue.push(make_pair(root,)); int templen = ;
TreeNode* topNode; while(!nodeQueue.empty())
{
topNode = nodeQueue.front().first;
templen = nodeQueue.front().second;
nodeQueue.pop(); if(topNode->left == NULL && topNode->right == NULL)
return templen; if(topNode->left)
nodeQueue.push(make_pair(topNode->left,templen+));
if(topNode->right)
nodeQueue.push(make_pair(topNode->right,templen+)); } }
}; int main()
{
TreeNode *n0 = new TreeNode();
TreeNode *n1 = new TreeNode();
n0->left = n1;
TreeNode *n2,*n3;;
Solution mysolution;
cout<<mysolution.minDepth(NULL); return ; }
LeetCode OJ——Minimum Depth of Binary Tree的更多相关文章
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- 【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] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [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][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 ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- 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
problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...
- 【leetcode】Minimum Depth of Binary Tree
题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
随机推荐
- Unity基础-Input接口
input 底层的设备输入接口,在开发中很少用到 Input.GetKey() // Update is called once per frame void Update () { if (Inpu ...
- jenkins+svn+pipeline+kubernetes部署java应用(二)
在jenkins中只能通过http的方式获取svn的数据,所以需要配置svn的http访问方式 一.安装http服务端和mod_dav_svn插件 由于Subversion需要版本化的控制,因此标准的 ...
- C++中重载、覆盖和隐藏的区别,以及适用场景
一.重载.覆盖和隐藏的区别 二.适用场景 1.重载: 适用于不同的数据类型都需要使用到的功能函数.以数据相加的函数为例,可以在同一个文件内提供以下的重载函数以支持同样的功能: int add(int, ...
- MyEclipse访问MSSQL2008数据库
首先到微软网站http://www.microsoft.com/zh-cn/download/details.aspx?id=21599下载sqljdbc_3.0.1301.101_chs.exe, ...
- layui的upload组件使用以及上传阻止测试
背景:页面上一个按钮,点击弹出上传框,从按钮的方法代码开始写:处理未选择文件阻止上传:通过判断选择文件的数量,显示或隐藏上传按钮: 在js中定义: function uploadFile(){ la ...
- loj2256 「SNOI2017」英雄联盟
真的是裸背包啊-- #include <iostream> #include <cstdio> using namespace std; typedef long long l ...
- luogu2158 [SDOI2008]仪仗队 欧拉函数
点 $ (i,j) $ 会看不见当有 $ k|i $ 且 $ k|j$ 时. 然后就成了求欧拉函数了. #include <iostream> #include <cstring&g ...
- Oracle 表空间的日常维护与管理
目录 Oracle 表空间的日常维护与管理 1.创建数据表空间 2.创建临时表空间 3.创建 UNDO 表空间 4.表空间的扩展与修改大小 5.表空间重命名 6.表空间的删除 7.更改表空间的读写模式 ...
- 到底有没有必要兼容IE版本
我就说两个字:"没有". 理由如下: 1.占资源空间,额外去写css hack去做页面兼容处理.(主要是增加css代码) PS:css hack 不是W3C的规范,css hack ...
- kNN的维数灾难与PCA降维
主成分分析 PCA 协方差矩阵 假设我们有 \[ X = \begin{pmatrix}X_1\\X_2\\\vdots\\X_m\end{pmatrix}\in\mathbb{R}^{m\times ...