题目描述:

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.

解法一:非递归方式,使用层序遍历的方法,借助队列,思想比较简单。

int minDepth(TreeNode* root)
{
int res = ;
if(!root)
return res;
else
{
res = ;
queue<TreeNode*> q;
q.push(root);
int layer_size_left = q.size(); while(!q.empty())
{
TreeNode * temp = q.front();
q.pop();
-- layer_size_left;
if(temp->left)
q.push(temp->left); if(temp->right)
q.push(temp->right); if(!temp->left && !temp->right)
return res; if(layer_size_left == )
{
++ res;
layer_size_left = q.size();
}
}
}
return res; }

解法二:递归方式,递归方式看上去更简洁一些,而且更易扩展,比如稍作修改即可解出最大深度等,当然非递归方式使用层序遍历解最大深度也不难。

int minDepth(TreeNode* root)
{
if(!root)
return ;
int l = minDepth(root->left);
int r = minDepth(root->right); if(!l)
return + r;
if(!r)
return + l; return l > r ? + r : + l;
}

两者的运行效率差不太多,leetcode上显示运行时间都是9ms,但是都不是最快的解法,应该还有可以优化的地方!这应该是我接下来努力的方向。

leetcode 111的更多相关文章

  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 ...

  2. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  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 ...

  4. Java实现 LeetCode 111 二叉树的最小深度

    111. 二叉树的最小深度 给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,nu ...

  5. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  6. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

  7. leetcode 111 Minimum Depth of Binary Tree ----- java

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  8. Java [Leetcode 111]Minimum Depth of Binary Tree

    题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

  9. [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)

    问题 给出一棵二叉树,找出它的最小深度. 最小深度是指从根节点沿着最短路径下降到最近的叶子节点所经过的节点数. 初始思路 不难看出又是一个需要层次遍历二叉树的题目,只要在112基础上作出简单修改即可得 ...

随机推荐

  1. 51Nod 1001 数组中和等于K的数对 Label:Water

    给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A:{-1,6,5,3,4,2,9,0,8},所有和等于8的数对包括(-1,9),(0 ...

  2. 在Linux中安装Tomcat

    带Linux的虚拟机中安装Tomcat 一.从官方网站上下载tomcat软件包.http://tomcat.apache.org/ apache-tomcat-7.0.33.tar.gz 二.下载到本 ...

  3. 学习PHP第一天-----简单登录

    <!DOCTYPE html> <html> <head> <title>初级登录界面</title> </head> < ...

  4. jsp页面价格

    <div class='flo_left'> <h2 class='red'>订货价¥${productEntity.agentsPrice}</h2> <h ...

  5. 收集的github的东西

    1.voghDev/PdfViewPager-Android widget that can render PDF documents stored on SD card, linked as ass ...

  6. [CareerCup] 16.5 Semphore 信号旗

    16.5 Suppose we have the following code:public class Foo { public Foo() { . . . } public void first( ...

  7. for循环练习

    1.输入一个整数,计算从1加到这个数的结果int sum = 0;Console.WriteLine("请输入一个正整数");int a = int.Parse(Console.R ...

  8. daterangepicker 日期范围插件自定义 可选 年份

    minDate:'01/01/2012',maxDate:'01/01/2015' $("#txtPODate").daterangepicker({ singleDatePick ...

  9. Uc爆破工具

    #coding:utf-8 import requests import sys from threading import Thread from Queue import Queue NUM=5 ...

  10. Spring 定时器

    <!-- 对定时任务进行引用 --> <bean id="schedulerFactory" class="org.springframework.sc ...