Beispiel #1
0
/* returns 1 if children sum property holds for the given
    node and both of its children*/
int isSumProperty(struct node* node)
{
  /* left_data is left child data and right_data is for right child data*/
  int left_data = 0,  right_data = 0;
 
  /* If node is NULL or it's a leaf node then
     return true */
  if(node == NULL ||
     (node->left == NULL && node->right == NULL))
    return 1;
  else
  {
    /* If left child is not present then 0 is used
       as data of left child */
    if(node->left != NULL)
      left_data = node->left->data;
 
    /* If right child is not present then 0 is used
      as data of right child */
    if(node->right != NULL)
      right_data = node->right->data;
 
    /* if the node and both of its children satisfy the
       property return 1 else 0*/
    if((node->data == left_data + right_data)&&
        isSumProperty(node->left) &&
        isSumProperty(node->right))
      return 1;
    else
      return 0;
  }
}
Beispiel #2
0
int isSumProperty(struct node* root)
{
	if(root==NULL)
		return 1;
	if(root-> left && root->right)
	{
		if(root->left->data + root->right->data==root->data)
			return (isSumProperty(root->left) && isSumProperty(root->right));
		else
			return 0;
	}
	else if(root->left==NULL && root->right!=NULL)
	{
		if(root->right->data==root->data)
			return isSumProperty(root->right);
		else
			return 0;
	}
	else if(root->right==NULL && root->left!=NULL)
	{
		if(root->left->data==root->data)
			return isSumProperty(root->left);
		else
			return 0;
	}
	else
		return 1;


}
Beispiel #3
0
bool isSumProperty(struct node *root){
	int rightNode = 0;
	int leftNode = 0;
	if (root == NULL || root->right == NULL && root->left == NULL)
		return 1;
	if (root->left != NULL)
		leftNode = root->left->data;
	if (root->right != NULL)
		rightNode = root->right->data;
	else if ((root->data == leftNode + rightNode) && isSumProperty(root->left) && isSumProperty(root->right))
		return true;
	return false;
}
Beispiel #4
0
int main(){
	struct node* root = createNode(0);
	root->left = createNode(1);
	root->right = createNode(2);
	root->left->left = createNode(3);
	root->right->left = createNode(4);
	if (isSumProperty(root))
		printf("True");
	else printf("False");
}
Beispiel #5
0
/* Driver program to test above function */
int main()
{
  struct node *root  = newNode(10);
  root->left         = newNode(8);
  root->right        = newNode(2);
  root->left->left   = newNode(3);
  root->left->right  = newNode(5);
  root->right->right = newNode(2);
  if(isSumProperty(root))
    printf("The given tree satisfies the children sum property ");
  else
    printf("The given tree does not satisfy the children sum property ");
 
  getchar();
  return 0;
}