面试题19:如何判断一棵二叉树是完全二叉树?
参考:http://blog.csdn.net/njdragonfly/article/details/6373199
1 struct TreeNode { 2 int val; 3 TreeNode* left; 4 TreeNode* right; 5 TreeNode() : 6 val(0), left(nullptr), right(nullptr) { 7 } 8 TreeNode(int x) : 9 val(x), left(nullptr), right(nullptr) { 10 } 11 }; 12 class Solution { 13 public: 14 bool isCompleteTree(TreeNode* root){ 15 if(root == nullptr) return true; 16 queue<TreeNode* > q; 17 TreeNode* p = root; 18 q.push(root); 19 while(p){ 20 q.pop(); 21 q.push(p->left); 22 q.push(p->right); 23 p = q.front(); 24 } 25 while(!q.empty()){ 26 p = q.front(); 27 q.pop(); 28 if(p != nullptr) return false; 29 } 30 return true; 31 } 32 };