/*采用递归法求解。*/
class Solution {
public:
int minDepth(TreeNode* root) {
if(root == nullptr) return 0;
if(root->left == nullptr && root->right == 0) return 1;
if(root->left && !root->right) return 1 + minDepth(root->left);
if(root->right && !root->left) return 1 + minDepth(root->right);
if(root->left && root->right) return 1 + min(minDepth(root->left), minDepth(root->right));
}
};LeetCode之Minimum Depth of Binary Tree
最新推荐文章于 2020-02-19 02:21:51 发布
本文介绍了一种使用递归算法来计算二叉树最小深度的方法。该算法通过检查根节点及其左右子节点的状态,递归地确定整个二叉树的最小深度。
1361

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



