题目描述:

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. The Skins of the Substance

    This blog is about a java jar file : Substance.jar well, you can get it from links as below: http:// ...

  2. Java_JDK动态代理学习笔记

    昨天被问了个问题,问题的大意是这样的:为什么 Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, Invoc ...

  3. 最好的文本框样式 最漂亮的文本框样式 textbox css样式

    输入框景背景透明: <input style="background:transparent;border:1px solid #ffffff"> 鼠标划过输入框,输入 ...

  4. 20145330孙文馨 《Java程序设计》第一周学习总结

    20145330孙文馨 <Java程序设计>第一周学习总结 教材学习内容总结 刚开始拿到这么厚一本书说没有压力是不可能的,开始从头看觉得很陌生进入不了状态,就稍微会有一点焦虑的感觉.于是就 ...

  5. 再说memcache的multiget hole(无底洞)

    关键词:multiget hole,memcache 适用于:java,php 基础知识背景: 1)multiget 是什么:     multiget 指的是从 memcache(或其他分布式缓存) ...

  6. linux top 参数详解

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法. top - 01:06:48 up  1:22,   ...

  7. WebApp之Meta标签

    <meta name="apple-touch-fullscreen" content="yes">"添加到主屏幕“后,全屏显示 < ...

  8. Pixar Shorts 皮克斯动画短片全集

    [原创短片](Theatrical Shorts)16部 <安德鲁和威利冒险记><顽皮跳跳灯><红色的梦><锡铁小兵><小雪人大行动>< ...

  9. java web(一) 使用sql标签库+tomcat+mysql手动创建一个jsp练习总结

    2016-09-0111:06:53                                     使用sql标签库+tomcat+mysql手动创建一个jsp 1. 1.1安装tomcat ...

  10. 从客户端(Content="<p>SF</p>")中检测到有潜在危险的 Request.Form 值

    出现这个问题,一般是由于.Net Framework 使用了4.0或以上版本造成的.(从 .Net Framework 4.0 开始,ASP.NET开始强制检测Request参数安全),而我们可以通过 ...