26. 二叉树的最小深度 发表于 2019-02-12 更新于 2025-04-02 分类于 programming 题目 解析 解析图: 首先,如果是Null,那么,便返回0 然后,看左子树深度和右子树的深度 接着,如果有左子树或者右子树的深度为0,那么返回另一个深度+1 最后,如果左右子树的深度都不为0,那么返回最小值+1 代码123456789int minDepth(TreeNode* root) { if(!root) return 0; int leftNum = minDepth(root -> left); int rightNum = minDepth(root -> right); if(!leftNum || !rightNum) return (leftNum + rightNum + 1); return min(leftNum, rightNum) + 1;}