您的位置:首页 > 其它

Minimum Depth of Binary Tree 二叉树的最小深度

2014-01-20 16:10 459 查看
/**

* 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;

else if(root->left&&root->right==NULL)

return minDepth(root->left)+1;

else if(root->right&&root->left==NULL)

return minDepth(root->right)+1;

else if(root->left&&root->right)

{

int left=minDepth(root->left);

int right=minDepth(root->right);

return (left<right?left:right)+1;

}

}

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: