一.题目

Minimum Depth of Binary Tree

Total Accepted: 58982 Total Submissions: 202860My
Submissions

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.

Show Tags
Have you met this question in a real interview?

Yes
No

Discuss








二.解题技巧

    这道题仅仅是一道二叉树的深度优先搜索。然后返回深度最小的值,能够递归来实现。递归退出的条件是到达叶子节点或者到达空子树,使用空子树作为退出条件比較easy进行推断。仅仅要该结点的指针值为NULL。就能够推断了,空子树的深度为0。

因此能够将每一个结点的左右两个子树的深度返回给父节点,父节点选择比較小的深度,然后再返回给祖先结点,以此类推,最后返回给根结点,得到终于结果。

    上面提到的这样的方法的时间复杂度为O(n),空间复杂度为O(logn)。


三.实现代码

#include <iostream>

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution
{
public:
int minDepth(TreeNode* root)
{
if (!root)
{
return 0;
} int Result = 1;
int Left = minDepth(root->left);
int Right = minDepth(root->right); if (Left * Right)
{
Result += Left > Right? Right : Left;
}
else
{
Result += Right + Left;
} return Result;
}
};


四.体会

    这是一道对二叉树进行递归来获得结果的题。我发如今眼下所做的二叉树的题目中,基本上考察的都是递归方面。预计这个也是二叉树的一个考点所在。




版权全部,欢迎转载,转载请注明出处,谢谢




LeetCode_Minimum Depth of Binary Tree的更多相关文章

  1. [LeetCode] 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] Maximum Depth of Binary Tree 二叉树的最大深度

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

  3. [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

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

  5. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  6. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  7. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

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

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

  9. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

随机推荐

  1. kotlin扩展函数

    kotlin 扩展函数 https://www.kotlincn.net/docs/reference/extensions.html 扩展函数在android中的应用 fun AppCompatAc ...

  2. MySQL索引,MySQL性能分析及explain的使用,分析SQL查询性能

    可以使用explain来分析MySQL查询性能,举例如下: 1.使用explain语句去查看分析结果 如 explain select * from test1 where id=1; 会出现: id ...

  3. Android录制视频报错setVideoSize called in a invalid state 1

    录制视频时想获取手机支持的录制视频的分辨率,使用代码如下: List<Camera.Size> videoSize = camera.getParameters().getSupporte ...

  4. WinForm-SQL查询避免UI卡死

    使用委托,呵呵. Func<List<VM>> act = () => { SqlDataReader reader = DBHelper.ExecuteReader(s ...

  5. 代码验证C#执行”文件打开关闭操作“耗时

    2017-04-19 部门经理习惯用C#做数据清洗,遇到个需要验证的问题,在一个万次左右循环内对文件执行打开关闭操作,比在循环前打开文件.循环后关闭文件耗时多多少. using System; usi ...

  6. eclipse自动添加作者、日期等注释

    使用eclipse的时候一般会添加自己的注释,标注日期作者等内容,我总结的添加注释的方式有两种:一.在新建class时自动添加注释:二.通过快捷键自动添加注释.下面分别描述一下添加方式. 一.新建cl ...

  7. Python线程操作

    一.全局锁 1.在Python中,Python代码的执行由Python虚拟机来控制,而在Python虚拟机中,同一时刻只有一个线程在执行,就像单CPU的系统中运行多个进程那样,内存中可以存放多个程序, ...

  8. 系统封装 如何打造原生WINPE

    1 安装微软的AIK(Windows Automated Installation Kit,Windows自动安装工具包),AIK简体中文版下载地址: http://download.microsof ...

  9. defer,panic,recover

    Go语言不支持传统的 try…catch…finally 这种异常,因为Go语言的设计者们认为,将异常与控制结构混在一起会很容易使得代码变得混乱.因为开发者很容易滥用异常,甚至一个小小的错误都抛出一个 ...

  10. Linux非阻塞IO(七)使用epoll重新实现客户端

    使用poll与epoll的区别主要在于: poll可以每次重新装填fd数组,但是epoll的fd是一开始就加入了,不可能每次都重新加入 于是采用这种策略: epoll除了listenfd一开始就监听r ...