-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
66 changed files
with
2,905 additions
and
245 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,69 @@ | ||
// Is the binary tree perfect | ||
// 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 | ||
// 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); | ||
} |
Oops, something went wrong.