leetcode_question_111 Minimum Depth of Binary Tree

来源:互联网 发布:知彼定位软件 编辑:程序博客网 时间:2024/05/18 02:13

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

int minDepth(TreeNode *root) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        if(root == NULL) return 0;        queue<TreeNode*> que;        que.push(root);        int count = 1;        int depth = 0;        while(!que.empty())        {            TreeNode* tmp = que.front();            que.pop();            count--;                        if(tmp->left == NULL && tmp->right == NULL) break;                        if(tmp->left)                que.push(tmp->left);            if(tmp->right)                que.push(tmp->right);                        if(count == 0){                depth++;                count = que.size();            }        }        return ++depth;    }


原创粉丝点击