最开始忽略了左子树为空还得计算右子树的叶子节点深度,直接分为左空右空和其他,对于其他认为直接返回0,导致会直接归入其他而报错! 改正后效果也挺一般: package leetcode01; //给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 publ
class Solution { public int minDepth(TreeNode root) { if (root == null){ return 0; } int left = minDepth(root.left); int right = minDepth(root.right); /** * 如果左右孩子有一个没有,那就只计算另
二叉树的最小深度 题目描述:给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明:叶子节点是指没有子节点的节点。 示例说明请见LeetCode官网。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/
Leetcode 111 二叉树最小深度 给定一颗二叉树,求根节点到叶子节点的最短路径(最小深度) class Solution{ public int minDepth(TreeNode root){ //若无根节点(空树),则返回0 if(root==null) { return 0; } //叶子节点,向上返回1,递归
给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明: 叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回它的最小深度 2. /** * Definition for
题目: 解答: 1 /** 2 * Definition for a binary tree node. 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
111. Minimum Depth of Binary Tree 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 leaf node. Note: A leaf is a node with no children. Example
104.最大深度 class Solution { public int maxDepth(TreeNode root) { //结点为空返回0 if(root==null){ return 0 ; }else{ //递归左子树 int ld = maxDepth(root.left); //递归右
# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def minDepth(self, root: TreeNode) -> int: if root is None:
file_dir=`find 目录 -mindepth 1 -maxdepth 5 - type d`for dir in $file_dirdo file_name=`basename $dir` if [ $file_name != "目标文件名" ];then rm -rf $dir if [ $? != 0 ];then echo "未删除成功!" exit -2 fi fidon
题目链接 : https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/ 题目描述: 给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明: 叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,null,null,15,7], 3
这道题是LeetCode里的第111道题。 题目描述: 给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明: 叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返
104: class Solution {public: int maxDepth(TreeNode* root) { if(root == NULL) return 0; int left = maxDepth(root->left); int right = maxDepth(root->right); return (left > right ? left : right) + 1; }};
深搜,左分支或右分支不存在时,找另一边,当到达叶子结点时返回0,两边都有分支时取当中最小值。顶层也算一层所以每次递归(划分成新的树)时都要进行+1 代码 class Solution { public:int minDepth(TreeNode* root) { if (root == NULL) { return 0; }