题目描述:

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. ACM: HDU 1028 Ignatius and the Princess III-DP

     HDU 1028 Ignatius and the Princess III Time Limit:1000MS     Memory Limit:32768KB     64bit IO Form ...

  2. 【BZOJ1179】 [Apio2009]Atm tarjan缩点+SPFA

    Description Input 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口 ...

  3. php 小试 mysql-zmq-plugin 和 pthreads

    原文: http://my.oschina.net/neochen/blog/294354 https://github.com/netkiller/mysql-zmq-plugin 有2张表: 表1 ...

  4. GO语言练习:值与引用

    1.代码 2.运行 package main import "fmt" func testValue(){ fmt.Println("for value") v ...

  5. 【原创】windows下搭建vue开发环境+IIS部署

    [原创]win10下搭建vue开发环境  如果要转发,请注明原作者和原产地,谢谢! 特别说明:下面任何命令都是在windows的命令行工具下进行输入,打开命令行工具的快捷方式如下图:     详细的安 ...

  6. hive中导入json格式的数据(hive分区表)

    hive中建立外部分区表,外部数据格式是json的如何导入呢? json格式的数据表不必含有分区字段,只需要在hdfs目录结构中体现出分区就可以了 This is all according to t ...

  7. AsyncTask的使用

    简单的AnsyTask的使用demo 1.定义一个模拟网络操作的类 package com.example.administrator.myapplication; /** * Created by ...

  8. 做为一名dba你应该知道这些数据恢复

    1.将备份数据   拉取到本地虚拟机上 进行恢复(千万不要把数据直接恢复到生产中,除非迫不得已!!)   2.在本地虚拟机上恢复之后,导出需要恢复的数据.   3.在本地虚拟机上恢复做恢复测试.   ...

  9. SQL存储过程生成顺序编码

    一.第一种方式 USE [WJKC]GO/****** Object:  StoredProcedure [dbo].[Address_GetCode1]    Script Date: 2016/3 ...

  10. 实时输出TextField中内容

    要想实时输出TextField中内容,要找到textField内容发现改变就会调用的函数,即 - (BOOL)textField:(UITextField *)textField shouldChan ...