Every day a leetcode
题目来源:111. 二叉树的最小深度
解法1:递归
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
#define INT_MAX 0x7fffffff
int min(int a,int b)
{
return a>b?b:a;
}
int minDepth(struct TreeNode* root){
if(root == NULL) return 0;
if(!root->left && !root->right) return 1;
int mindepth=INT_MAX;
if(root->left) mindepth=min(mindepth,minDepth(root->left));
if(root->right) mindepth=min(mindepth,minDepth(root->right));
return mindepth+1;
}
结果:

这篇博客介绍了如何用递归方法解决LeetCode上的111题——二叉树的最小深度。作者提供了C语言实现的递归代码,通过检查根节点及其左右子树来计算最小深度。当遇到空节点时返回0,若左右子树都存在则取较小深度加1作为当前节点的最小深度。
430

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



