/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
if(root==NULL){
return 0;
}
else if(root->left!=NULL&&root->right==NULL){
return 1+minDepth(root->left);
}
else if(root->left==NULL&&root->right!=NULL){
return 1+minDepth(root->right);
}
else{
return 1+min(minDepth(root->left),minDepth(root->right));
}
}
};
LeetCode - 111 Minimum Depth of Binary Tree
最新推荐文章于 2019-03-25 22:14:51 发布