Python3代码 # Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = NoneclassSolution:defminDepth(self,root:TreeNode)->int:# solution one: DFS# 根为空ifnotroot:return0# 左右子树都为空ifnotroot.leftandnotroot...
111. Minimum Depth of Binary Tree(二叉树的最小深度) 题目链接:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明: 叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,null,null...
最小深度是指树的根结点到最近叶子结点的最短路径上结点的数量。 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: 输入: (1,2,3,4,5) 输出:2 代码: 1/**2* struct Tr...
publicclassMinimumDepthofBinaryTree { //递归,树的最小深度,就是它左右子树的最小深度的最小值+1 publicintminDepth(TreeNode root) { if(root ==null) { return0; } intlmin = minDepth(root.left); intrmin = minDepth(root.right); if(lmin ==0&& rmin ==0) { return1; } if(lmin ==0...
public int maxDepth(TreeNode root) { if (root == null) { return 0; } return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; } 这道题是不是只要把Math.max,改成Math.min就够了。 public int minDepth(TreeNode root) { if (root == null) { return 0; } return Math.min...
题目: 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 ... 查看原文 [LeetCode]111.Minimum Depth of Binary Tree ...
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,null,15,7], 3 / \ 9 20
LeetCode 111 题,Minimum Depth of Binary Tree 解题思路 1、读题,求解二叉树的最小Depth。 2、用bfs 去解决,队列带上每层的层数。当有一个节点的左右子树都为空时,返回当前层数,就找到解。 Python 代码 # De…
Minimum Depth of Binary Tree 二叉树的最小深度 采用递归的方式求左右结点的高度,注意判断一个结点是否是叶子结点(左右子树都不存大)。 int minDepth(TreeNode *root) { return minDepth(root, false); } int minDepth(TreeNode *root, bool hasbrothers)...
class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; left = null; right = null; } } C# Copy The code is a C# function that returns the minimum depth of a binary tree. The depth of a tree is the number of nodes along the longest ...