【问题描述】:
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 run(TreeNode *root) {
if(root==NULL)
return 0;
if(root->left==NULL&&root->right==NULL)
return 1;
if(root->left==NULL)
return run(root->right)+1;
else if(root->right==NULL)
return run(root->left)+1;
else return min(run(root->left),run(root->right))+1;
}
};