111Minimum Depth of Binary Tree

来源:互联网 发布:linux重启网络服务失败 编辑:程序博客网 时间:2024/06/05 17:46
方法一:(深度优先搜索,记得单个结点深度为1,注意边界情况。)class Solution {public:    int minDepth(TreeNode *root) {        if(root==0)            return 0;        if(root->left==NULL&&root->right==NULL)            return 1;                int l=0x3f3f3f3f,r=0x3f3f3f3f;        if(root->left!=NULL)            l=1+minDepth(root->left);        if(root->right!=NULL)            r=1+minDepth(root->right);        return min(l,r);    }};

0 0
原创粉丝点击