描述
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.
代码
class Solution {
public:
//int min=88888888;
int run(TreeNode *root) {
if(root==NULL){
return 0;
}
int left=run(root->left);
int right=run(root->right);
if(left==0||right==0){
return left+right+1;
}
return 1+min(left,right);
}
int min(int a,int b){
return a<b?a:b;
}
};
本文介绍了一种求解二叉树最小深度的算法。该算法通过递归方式遍历二叉树,寻找从根节点到最近叶节点的最短路径,并返回此路径上的节点数。适用于计算机科学与数据结构的学习。
439

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



