Exemple #1
0
int isSumTree(struct TNode* node)
{
    int ls; // for sum of nodes in left subtree
    int rs; // for sum of nodes in right subtree
    if(node == NULL || IsLeafNode(node))
        return 1;

    if( isSumTree(node->left) && isSumTree(node->right))
    {
        if(node->left == NULL)
            ls = 0;
        else if(IsLeafNode(node->left))
            ls = node->left->data;
        else
            ls = 2*(node->left->data);
        if(node->right == NULL)
            rs = 0;
        else if(IsLeafNode(node->right))
            rs = node->right->data;
        else
            rs = 2*(node->right->data);
        return(node->data == ls + rs);
    }

    return 0;
}
Exemple #2
0
int isSumTree(struct node* node)
{
    int ls, rs;
    if(node == NULL||(node->left == NULL && node->right == NULL))
        return 1;
   ls = sumnode(node->left);
   rs = sumnode(node->right);
  if((node->data== (ls+rs))&& isSumTree(node->left) && isSumTree(node->right))
        return 1;
 return 0;
}
Exemple #3
0
int isSumTree(struct node* root){
	//its a sum tree
	if(root == NULL || (root->left == NULL && root->right == NULL) ){
		return 1;
	}
	
	int suml = sum(root->left);
	int sumr = sum(root->right);
	printf("sum of left and right subtree %d %d\n",suml, sumr );
	if(((suml + sumr) == root->item) && isSumTree(root->left) && isSumTree(root->right)){
		return 1;
	}
	return 0;
}
int isSumTree(struct node* node)
{
	int lefttree, righttree;
	if(node ==0 ||(node->left == 0 && node->right == 0))
	{
		return 1;
	}
	lefttree = sum(node->left);
    righttree = sum(node->right);
	if((node->data == lefttree + righttree)&&isSumTree(node->left) && isSumTree(node->right))
	{
		return 1;
	}

	return 0;
}
Exemple #5
0
int main(){


struct TNode* root=NULL;
printf("\nEnter the number of elements you want to insert : ");
int n;
scanf("%d",&n);

int i,data;
for(i=0;i<n;i++){
printf("\nEnter data : ");
scanf("%d",&data);
InsertBinaryTree(&root,data,n);
}

printf("\nInserted... %d nodes \n",n);

getch();

printf("\nPress enter to do level-order traversal : \n\n");
getch();
LevelOrderTraversal(root,n);
getch();
int result=isSumTree(root);
if(result){
printf("\n\nSum Tree");
}else{
printf("\nNot a sum tree");
}
return 0;
}
Exemple #6
0
int main()
{
    struct node *root  = newNode(26);
    root->left         = newNode(10);
    root->right        = newNode(3);
    root->left->left   = newNode(4);
    root->left->right  = newNode(6);
    root->right->right = newNode(3);
    if(isSumTree(root))
        printf("The given tree is a SumTree ");
    else
        printf("The given tree is not a SumTree ");
    getchar();
    return 0;
}
Exemple #7
0
int main(){
	struct node* root = build_node(26);
	root->left = build_node(10);
	root->right = build_node(3);
	root->left->left = build_node(4);
	root->left->right = build_node(6);
	
	root->right->right = build_node(3);
	
	if(isSumTree(root) == 1){
		printf("Yes");
	}
	else{
		printf("No");
	}
}
int main()
{
	struct node *root = insert(26);
	root->left		 = insert(10);
	root->right	 = insert(3);
	root->left->left = insert(4);
	root->left->right = insert(6);
	root->right->right = insert(3);
	if(isSumTree(root))
	{
		printf("Binary tree is a sumtree");
	}
	else
	{
		printf("Binary tree is not a sumtree");
	}
	return 0;
}