leetcode笔记:Minimum Depth of Binary Tree

一. 题目描述

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.

二. 题目分析

这道题属于二叉树的深度优先搜索,然后返回深度最小的值,可以递归(当然,也可以使用迭代)来实现。递归退出的条件是到达叶子节点或者到达空子树,使用空子树作为退出条件比较容易进行判断,只要该结点的指针值为NULL时,就可以判断空子树的深度为0。因此,可以将每个结点的左右两个子树的深度返回给父节点,父节点选择比较小的深度,然后再返回给祖先结点,以此类推,最后返回给根结点,得到最终结果。

这种方法的时间复杂度为:O(n),空间复杂度为:O(logn)

三. 示例代码

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;
    }
};

四. 小结

这是一道对二叉树进行递归来得到结果的题,递归在二叉树的相关题目中还是考察得比较多的。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-02 05:05:35

leetcode笔记:Minimum Depth of Binary Tree的相关文章

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree 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. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

Leetcode 111. Minimum Depth of Binary Tree

111. Minimum Depth of Binary Tree Total Accepted: 114343 Total Submissions:368035 Difficulty: Easy 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 le

[Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree

Minimum Depth of Binary Tree 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. 比较左子树和右子树的深度,取小的那个返回上来,并+1. 需要注意的是如果没有左子树或右子树.那么无条件取存在的那一边子树深

leetcode dfs Minimum Depth of Binary Tree

Minimum Depth of Binary Tree Total Accepted: 25609 Total Submissions: 86491My 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.

LeetCode:Minimum Depth of Binary Tree

要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意(l<r)? l+1:r+1的区别应用,因为可能存在左右子树为空的情况,此时值就为0,但显然值是不为0的(只有当二叉树为空才为0),所以,在这里注意一下即可! 代码如下: 1 struct TreeNode { 2 int val; 3 TreeNode *left; 4 TreeNode *rig

[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 shortest path from the root node down to the nearest leaf node. 题意及分析:求一棵二叉树的最低高度,即根节点到叶子节点的最短路径.遍历二叉树,然后用一个变量保存遍历到当前节点的最小路径即可,然后每次遇到叶子节点,就判断当前叶节点高度和先

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 the shortest path from the root node down to the nearest leaf node. 解题思路: 递归. 代码描述: /** * Definition for a binary tree node. * public class TreeNode { *

Java for 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 shortest path from the root node down to the nearest leaf node. 解题思路:注意minimum depth最后遍历的那个点,left right都必须为null,JAVA实现如下: public int minDepth(TreeNode roo

[LeetCode][Java] Minimum Depth of Binary Tree

题目: 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. 题意: 给定一棵二叉树.返回它的最小高度. 最小高度是指从根节点到近期的叶子节点的最短路径中的节点的数目. 算法分析: * 借助堆 * 类似<Binary Tree Lev

LeetCode – Refresh – Minimum Depth of Binary Tree

1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 int minDepth(TreeNode *root) { 13