最小深度是指树的根结点到最近叶子结点的最短路径上结点的数量。 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...
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...
# 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.right:...
题目链接:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明: 叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,null,null,15,7], 返回它的最小深度 2. AC 0ms...111...
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 children. Example: Given binary tree[3,9,20,null,null,15,7], ...
LeetCode 111 题,Minimum Depth of Binary Tree 解题思路 1、读题,求解二叉树的最小Depth。 2、用bfs 去解决,队列带上每层的层数。当有一个节点的左右子树都为空时,返回当前层数,就找到解。 Python 代码 # De…
题目: Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-no... 979. Distribute Coins in Binary Tree——depth ...
int mright=minDepth(root->right); if(mleft==0) return 1+mright; else if(mright==0) return 1+mleft; else return min(mleft,mright)+1; } }; 二、 * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; ...
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 ...