您的位置:首页 > 其它

leetcode 111. Minimum Depth of Binary Tree

2016-08-21 15:33 387 查看
/**
* 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 dep;
void dfs(TreeNode*root,int d){
if(d >= dep || root == NULL) return ;
if(root->left == NULL && root->right == NULL){
dep = d;
return ;
}
dfs(root->left,d+1);
dfs(root->right,d+1);
}
int minDepth(TreeNode* root) {
dep = 1000000000;
if(root == NULL) return 0;
dfs(root,1);
return dep;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode