111. Minimum Depth of Binary Tree

ゝ一纸荒年。 2022-06-10 14:10 291阅读 0赞

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.

32%

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int minDepth(struct TreeNode* root) {
int i,j;
if(!root)
return 0;
i = minDepth(root->left);
j = minDepth(root->right);
return i<j && i || !j ?i+1:j+1;
}

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读