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 ...
随机推荐
- 【单调队列】P1886 滑动窗口
GET 单调队列 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: Th ...
- linux常用命令(配置查看,定时任务)
1.查看所有待挂载设备信息 fdisk -l # fdisk -l Disk /dev/sda: bytes heads, sectors/track, cylinders, total sector ...
- GoF23种设计模式之行为型模式之访问者模式
概述 表示一个作用于某对象结构中的各元素的操作. 它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 适用性 1.一个对象结构包含很多类对象,它们有不同的接口,而你想对这些对象实施一些依 ...
- python列表中的深浅copy
列表中的赋值和平常的赋值是不一样的,看下面的代码: In [1]: a = 1 In [2]: b = a In [3]: a Out[3]: 1 In [4]: b Out[4]: 1 In [5] ...
- 稀疏表(ST / Sparse Table)
RMQ问题: 给定一个序列,每次询问一个区间最小值 / 最大值. 没有修改. //拿区间最大值来举例. memset(ans, -INF, sizeof(ans)); for (int i = 1; ...
- C++实现Behavioral - Observer模式 (转)
转http://patmusing.blog.163.com/blog/static/13583496020101501923571/ 也称为Dependents或Publish-Subscribe模 ...
- CDH4 journalnode方式手工安装手册之二
一. Hadoop配置修改 修改core-site.xml文件 <configuration> <property> ...
- Prolog&Epilog
这篇博客会简单介绍一下Prolog&Epilog 然后再简单介绍下我对于程序在计算机中到底如何运行的一些理解(因为自己之前也从来没有接触过这些方面的知识,所以如果有讲的不对的地方希望大家能够帮 ...
- JAVA-基础(二) java.lang
1.String类提供了许多从String对象中截取字符的方法 1.1 char charAt(int where) 1.2 void getChars(int sourceStart, int so ...
- Eclipse下创建Spring MVC web程序--maven版
1. 创建一个maven工程: File->New->Other... 2. 创建完成后的结构如下: 3. 配置pom.xml文件,添加spring-webmvc依赖项 <pro ...