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.
题解:注意没有的分支不算。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int dfs(TreeNode *r){
if(r==NULL) return ;
if(r->left==NULL&&r->right==NULL){
return ;
}
else{
if(r->left==NULL){
return dfs(r->right)+;
}
else if(r->right==NULL){
return dfs(r->left)+;
}
else{
return min(dfs(r->left),dfs(r->right)) + ;
}
}
} int minDepth(TreeNode* root) {
return dfs(root);
}
};