forked from wangcy6/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
009_tree_Full binary tree.c
99 lines (79 loc) · 2.21 KB
/
009_tree_Full binary tree.c
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*Check if the binary tree is a complete binary tree
*/
#include<iostream>
#include<conio.h>
using namespace std;
struct BinaryTree
{
int data;
struct BinaryTree *left;
struct BinaryTree *right;
};
typedef struct BinaryTree node;
node* insert(node *r,node *z)
{
if(!r)
return z;
if(z->data < r->data)
r->left=insert(r->left,z);
else
r->right=insert(r->right,z);
return r;
}
//完全二叉树:
/**
1 Calculate the number of nodes (count) in the binary tree.
2 Start recursion of the binary tree from the root node of the binary tree with index (i) being set as 0 and the number of nodes in the binary (count).
3 If the current node under examination is NULL, then the tree is a complete binary tree. Return true.
4 If index (i) of the current node is greater than or equal to the number of nodes in the binary tree (count) i.e. (i>= count), then the tree is not a complete binary. Return false.
5 Recursively check the left and right sub-trees of the binary tree for same condition.
For the left sub-tree use the index as (2*i + 1) while for the right sub-tree use the index as (2*i + 2).
6 The time complexity of the above algorithm is O(n).
**/
bool isCompleteBinary(node *root)
{
int total=get_node_number(root);
return is_complete_binary(root,1,total);
}
bool is_complete_binary(node *root,int index,int& length)
{
if(root ==NULL)
{
return true;
}
if(index>length)
{
return false;
}
return is_complete_binary(root->left,2*index,length) && is_complete_binary(root->right,2*index+1,length);
}
int get_node_number(node* root)
{
if(root ==NULL)
{
return 0;
}
return 1+get_node_number(root->left)+get_node_number(root->right);
}
bool isFullBinaryTree( node *root)
{
if(!root)
return true;
if((root->left && !root->right)||(root>right && !root->left))
return false;
return(isFullBinaryTree(R->lef)&&isFullBinaryTree(R->right));
}
int main()
{
int i,h=0;
node *root=0,*newnode;
int a[10]={5,9,7,3,1,4,12,13,0,6};
for(i=0;i<;7;i++)
{
newnode=new node;
newnode->data=a[i];
newnode->left=newnode->right=0;
root=insert(root,newnode);
}
cout<<isFullBinaryTree(root);
}