Minimum Depth of Binary Tree

https://leetcode.com/problems/minimum-depth-of-binary-tree/

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int minDepth(TreeNode* root) {
13         queue<TreeNode *> q1;
14         if(root==NULL)
15             return 0;
16         q1.push(root);
17         int depth=0;
18         bool flag=0;
19         while(!q1.empty())
20         {
21             depth++;
22             queue<TreeNode *> q2;
23             while(!q1.empty())
24             {
25                 TreeNode * temp=q1.front();
26                 q1.pop();
27                 if(temp->left==NULL&&temp->right==NULL)
28                 {
29                     flag=1;
30                     break;
31                 }
32                 if(temp->left!=NULL)
33                     q2.push(temp->left);
34                 if(temp->right!=NULL)
35                     q2.push(temp->right);
36             }
37             q1=q2;
38             if(flag==1)
39                 break;
40         }
41         return depth;
42     }
43 };
原文地址:https://www.cnblogs.com/aguai1992/p/4632573.html