判断是否为完全二叉树 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;
}

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

2016-04-30 21:23:13

【Leetcode】Given a binary tree, check whether it is a mirror of itself

2013-10-01 22:03:10

怎样判断一个二叉树是binary search tree

2012-05-17 23:57:38

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

2016-04-06 16:00:49

leetcode[101]:Symmetric Tree

2015-06-18 16:08:35

满二叉树(Full Binary Tree)&&完全二叉树(Complete Binary Tree)

2016-11-05 22:21:09

LeetCode222 Count CompleteTree Nodes(计算完全二叉树的节点数) Java 题解

2015-07-09 20:51:26

Leetcode: symmetric-tree

2017-05-26 16:34:21

04-树6 Complete Binary Search Tree 完全二叉搜索树

2015-10-24 23:02:19

LeetCode222——Count Complete Tree Nodes

2015-08-12 23:56:30