leetcode 111 Minimum Depth of Binary Tree

来源:互联网 发布:蜂窝数据漫游是什么 编辑:程序博客网 时间:2024/06/06 07:18


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.

Subscribe to see which companies asked this question


class Solution {public:int minDepth(TreeNode *root) {if(root==NULL) return 0;queue<TreeNode*> q;q.push(root);q.push(NULL);int height=1;while(!q.empty()) {TreeNode *temp = q.front();q.pop();if(temp!=NULL) {if(temp->left==NULL && temp->right==NULL) break;if(temp->left!=NULL)q.push(temp->left);if(temp->right!=NULL)q.push(temp->right);} else {height++;q.push(NULL);}}return height;}}



0 0
原创粉丝点击