-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisperfect_bt.cpp
69 lines (60 loc) · 1.45 KB
/
isperfect_bt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Is binary tree perfect
//A Binary tree is a Perfect Binary Tree in which all internal nodes have two children
// and all leaves are at the same level.
// NOT WORKING
#include <iostream>
#include<queue>
using namespace std;
struct node{
int data;
struct node* left;
struct node* right;
node(int value){
data = value;
left = NULL;
right = NULL;
}
};
void addEdge(struct node* root , int key){
if(root == NULL){
root = new node(key);
}else if(key > root->data){
addEdge(root->right, key);
}else{
addEdge(root->left, key);
}
}
// approach 1 :
// Iterative approach to check if a Binary Tree is Perfect
bool isperfect(node* root){
queue<node*> q;
q.push(root);
int flag=0;
while (!q.empty()){
node * temp = q.front();
q.pop();
if(temp->left && temp->right){
if(flag==1){
return false;
}else{
q.push(root->left);
q.push(root->right);
}
}else if(!temp->right && !temp->left){
flag=1;
}else if(!temp->right || !temp->left){
return false;
}
}
return true;
}
int main(){
// int arr[]={5,7,3,6,2,9};
int arr[]={1,2,3,4};
int n = sizeof(arr)/sizeof(arr[0]);
struct node* root= new node(arr[0]);
for(int i=1;i<n;i++){
addEdge(root,arr[i]);
}
cout << isperfect(root);
}