GVKun编程网logo

LeetCode(37)-Minimum Depth of Binary Tree

18

对于想了解LeetCode(37)-MinimumDepthofBinaryTree的读者,本文将是一篇不可错过的文章,并且为您提供关于#Leetcode#111.MinimumDepthofBina

对于想了解LeetCode(37)-Minimum Depth of Binary Tree的读者,本文将是一篇不可错过的文章,并且为您提供关于#Leetcode# 111. Minimum Depth of Binary Tree、154.Minimum Depth of Binary Tree、155. Minimum Depth of Binary Tree【LintCode by java】、1、minimum-depth-of-binary-tree的有价值信息。

本文目录一览:

LeetCode(37)-Minimum Depth of Binary Tree

LeetCode(37)-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 { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
public class Solution {
    public int minDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        if(root.left != null && root.right == null){
            return 1+minDepth(root.left);
        }else if(root.left == null && root.right != null){
            return 1+minDepth(root.right);
        }else{
            return 1+Math.min(minDepth(root.left),minDepth(root.right));
        }
    }
}

#Leetcode# 111. Minimum Depth of Binary Tree

#Leetcode# 111. Minimum Depth of Binary Tree

https://leetcode.com/problems/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.

Note: A leaf is a node with no children.

Example:

Given binary tree [3,9,20,null,15,7],

    3
   /   9  20
    /     15   7

return its minimum depth = 2.

代码:

/**
 * DeFinition for a binary tree node.
 * 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;
        if(!root -> left)
            return 1 + minDepth(root -> right);
        else if(!root -> right)
            return 1 + minDepth(root -> left);
        else
            return 1 + min(minDepth(root -> left),minDepth(root -> right));
    }
};

  Morning

154.Minimum Depth of Binary Tree

154.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.

最小深度是沿从根节点到最近的叶节点的最短路径上的节点数。

Note: A leaf is a node with no children.

注意:叶子是没有子节点的节点。

Example:

Given binary tree [3,9,20,null,15,7],

    3
   /   9  20
    /     15   7

return its minimum depth = 2.

解答:

 1 /**
 2  * DeFinition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public int minDepth(TreeNode root) {
12         if(root==null) return 0;
13         int left=minDepth(root.left);
14         int right=minDepth(root.right);
15         return (left==0 || right==0) ? left+right+1:Math.min(left,right)+1;
16     }
17 }

详解:

 DFS 栈 递归

155. Minimum Depth of Binary Tree【LintCode by java】

155. Minimum Depth of Binary Tree【LintCode by java】

Description

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.

Example

Given a binary tree as follow:

  1
 / \ 
2   3
   / \
  4   5  

The minimum depth is 2.

题意:求二叉树的最小深度。至于这个最小深度该怎么理解呢?接触的比较多的是最大深度,即根结点到离它最远的叶子结点的距离(包括首尾),这里的距离当然是指结点的个数。显然,最小深度就是根结点到离它最近的结点之间距离。虽然知道是这个意思,在一开始的理解上还有点偏差。例如,我想如果根结点只有右子树,那么这个最小深度怎么算,是不是就是1了,因为左子树为空嘛。后来仔细想想,其实不然,左子树为空的话,如果右子树不为空,说明根结点(其他结点亦然)不是叶子点,还得继续求右子树的深度(对应于代码的第26和29行),直到遇到叶子结点。代码如下:

 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12 
13 public class Solution {
14     /**
15      * @param root: The root of binary tree
16      * @return: An integer
17      */
18     public int minDepth(TreeNode root) {
19         // write your code here
20         if(root==null){
21             return 0;
22         }
23         if(root.left==null&&root.right==null){
24             return 1;
25         }
26         if(root.left==null){
27             return minDepth(root.right)+1;
28         }
29         if(root.right==null){
30             return minDepth(root.left)+1;
31         }
32         return Math.min(minDepth(root.left),minDepth(root.right))+1;
33     }
34 }

 

 

1、minimum-depth-of-binary-tree

1、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.
 
int run(TreeNode *root){
    if (root == nullptr) return 0;
    if (root->left == nullptr)
    {
        return run(root->right) + 1;
    }
    if (root->right == nullptr)
    {
        return run(root->left) + 1;
    }
    int leftDepth = run(root->left);
    int rightDepth = run(root->right);
    return (leftDepth <= rightDepth) ? (leftDepth + 1) : (rightDepth + 1);
}

今天的关于LeetCode(37)-Minimum Depth of Binary Tree的分享已经结束,谢谢您的关注,如果想了解更多关于#Leetcode# 111. Minimum Depth of Binary Tree、154.Minimum Depth of Binary Tree、155. Minimum Depth of Binary Tree【LintCode by java】、1、minimum-depth-of-binary-tree的相关知识,请在本站进行查询。

本文标签: