class Solution {
public:
int minDepth(TreeNode *root) {
if(root == NULL)
return 0;
if(root -> left == NULL)
return minDepth(root -> right) + 1;
if(root -> right == NULL)
return minDepth(root -> left) + 1;
return min((minDepth(root -> left) + 1),(minDepth(root -> right) + 1));
}
};[LeetCode] Minimum Depth of Binary Tree
最新推荐文章于 2023-12-15 03:22:29 发布
本文介绍了一个用于计算二叉树最小深度的递归算法。该算法通过检查根节点及左右子节点的状态来确定整棵树的最小深度。当根节点为空时返回0;若根节点的任一子节点为空,则继续在另一侧递归直到找到叶子节点。
445

被折叠的 条评论
为什么被折叠?



