美文网首页
Minimum-depth-of-binary-tree

Minimum-depth-of-binary-tree

作者: DaiMorph | 来源:发表于2019-05-30 00:47 被阅读0次

    层序遍历,如果有一个节点没有儿子节点,就输出它

    class Solution {
    public:
        int run(TreeNode *root) {
            if(root==NULL)return 0;
            int ans=1;
            queue<TreeNode*>q;
            q.push(root);
            while(!q.empty())
            {
                int size=q.size();
                while(size--)
                {
                    TreeNode*top=q.front();
                    q.pop();
                    if(top->left)q.push(top->left);
                    if(top->right)q.push(top->right);
                    if(!top->left&&!top->right)return ans;
                }
                ans++;
            }
            return ans;
        }
    };
    

    相关文章

      网友评论

          本文标题:Minimum-depth-of-binary-tree

          本文链接:https://www.haomeiwen.com/subject/bdeptctx.html