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) {
while
(root)
{
if
(!root->left && !root->right)
return
1
;
else
{
if
(root->left && root->right)
return
run(root->left)<run(root->right)?
1
+run(root->left):
1
+run(root->right);
if
(root->left && !root->right)
return
1
+run(root->left);
if
(root->right && !root->left)
return
1
+run(root->right);
}
}
return
0
;
}
};