一.题目

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. 分享Kali Linux 2017年第23周镜像文件

    分享Kali Linux 2017年第23周镜像文件  Kali Linux官方于6月4日发布2017年的第23周镜像.这次维持了11个镜像文件的规模.默认的Gnome桌面的4个镜像,E17.KDE. ...

  2. [BZOJ2095]Bridges

    最大值最小,是二分 转化为判定问题:给定一个混合图,问是否存在欧拉回路 首先,有向图存在欧拉回路的充要条件是每个点的入度等于出度,现在我们有一个混合图,我们要做的就是给其中的无向边定向,使得它变成有向 ...

  3. Intent创建Activity

    1,布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  4. kubernetes API Server安全

    用户访问API Server(以下简称Server),K8S的安全检查步骤:认证和授权. 认证解决用户是谁的问题,就是验证用户名密码;授权解决用户能做什么的问题,就是检查该用户是否拥有权限访问请求的资 ...

  5. [置顶] kubernetes资源对象--Label

    概念 Label机制是K8S中一个重要设计,通过Label进行对象弱关联,灵活地分类和选择不同服务或业务,让用户根据自己特定的组织结构以松耦合方式进行服务部署. Label是一对KV,对用户而言非常有 ...

  6. Python开发easy忽略的问题

    这篇文章主要介绍了Python程序猿代码编写时应该避免的17个"坑",也能够说成Python程序猿代码编写时应该避免的17个问题,须要的朋友能够參考下 一.不要使用可变对象作为函数 ...

  7. 创建es索引{"acknowledged"=>true, "shards_acknowledged"=>false}

    创建es索引{"acknowledged"=>true, "shards_acknowledged"=>false} [2018-05-19T13: ...

  8. spring自动装配(No qualifying bean )

    No qualifying bean of type [com.wfj.service.cms.main.ChannelMng] found for dependency: expected at l ...

  9. [Angular] Angular ngSwitch Core Directive In Detail

    When want to display different component based on some conditions: <div class='course-category' [ ...

  10. 重入锁的好搭档:Condition条件(读书笔记)

      Condition结构提供了基本方法如下: void await() throws InterruptedException; void awaitUninterruptibly(); long ...