判断是否为完全二叉树 Check whether a binary tree is a complete tree or not

//Tree.h

#ifndef TREE_H
#define TREE
struct TreeNode{
TreeNode *left;
TreeNode *right;
int val;
TreeNode(int);
};
#endif



//tree.cpp
#include<iostream>
#include "Tree.h"
TreeNode::TreeNode(int v):left(NULL),right(NULL),val(v){}

//main.cpp
#include<iostream>
#include "tree.h"
#include<queue>
using namespace std;

bool isCompleteTree(TreeNode *root){
if(root == NULL){
return true;
}
queue<TreeNode *> q;
q.push(root);
bool end = false;
while(!q.empty()){
TreeNode * node = q.front();
q.pop();
if(node->left && !end){
q.push(node->left);
}else if(!end && node->left == NULL){
end = true;
}else if(end && node->left){
return false;
}
if(!end && node->right){
q.push(node->right);
}else if(!end && node->right  == NULL){
end = false;
}else if(end &&  node->right ){
return false;
}
}
return true;

}
int main(){
TreeNode *root     = new TreeNode(1);
root->left         = new TreeNode(2);
root->right        = new TreeNode(3);
//root->right->left  = new TreeNode(10);
//root->left->left   = new TreeNode(4);
// root->left->right  = new TreeNode(5);
//root->right->right = new TreeNode(6);
cout << isCompleteTree(root)<<endl;
}

• 本文已收录于以下专栏：

1110. Complete Binary Tree (25) 完全二叉树、树的遍历

• a1eafall
• 2016年04月06日 16:00
• 1128

leetcode 101. Symmetric Tree 判断对称树,递归和迭代

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For ...
• chenchaofuck1
• 2016年04月30日 21:23
• 1061

Tree_Graph Validate Binary Search Tree 检测一个BST是否有效 @CareerCup

Implement a function to check if a binary tree is a binary search tree 判断一个二叉树是否是BST 思路： 1）中序遍历二叉树...
• hellobinfeng
• 2014年03月03日 08:56
• 1635

浙大 PAT 1043. Is It a Binary Search Tree (25)

1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限...
• IAccepted
• 2014年03月05日 13:23
• 2802

codeforces 792D——Paths in a Complete Binary Tree（二叉树，数学）

D. Paths in a Complete Binary Tree time limit per test 3 seconds memory limit per test 256 megab...
• say_c_box
• 2017年03月29日 22:34
• 400

D. Paths in a Complete Binary Tree

T is a complete binary tree consisting of n vertices. It means that exactly one vertex is a root, ...
• qq_33362864
• 2017年03月28日 22:01
• 202

pat甲1123. Is It a Complete AVL Tree (AVL树+层次遍历+完全二叉树)

pat advanced level 1123. Is It a Complete AVL Tree (30) pat甲1123. Is It a Complete AVL Tree An AV...
• qq_27601815
• 2016年12月13日 17:22
• 535

solution Of Pat 1110. Complete Binary Tree (25)

1110. Complete Binary Tree (25) Given a tree, you are supposed to tell if it is a complete binary t...
• qq_30490125
• 2016年03月15日 12:03
• 1157

leetcode 669. Trim a Binary Search Tree

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that ...
• huanghanqian
• 2017年09月04日 17:13
• 1278

Trim a Binary Search Tree问题及解法

669. Trim a Binary Search Tree LeetCode
• u011809767
• 2017年09月05日 11:55
• 691

举报原因： 您举报文章：判断是否为完全二叉树 Check whether a binary tree is a complete tree or not 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)