public static boolean what (Node n)
{
   if (n == null)
	return true;
   if (n.getLeftSon()==null && n.getRightSon()==null)
	return true;
   if (n.getRightSon()==null)
	return (n.getNumber() % n.getLeftSon().getNumber() == 0) 
			&&
        what (n.getLeftSon());
   if (n.getLeftSon()==null)
	return (n.getNumber() % n.getRightSon().getNumber() == 0) 
			&&
        what (n.getRightSon());

   return (n.getNumber() % n.getLeftSon().getNumber() == 0) 
     &&   (n.getNumber() % n.getRightSon().getNumber() == 0)
     &&	what (n.getLeftSon()) 
     &&	what (n.getLeftSon());
}

I think that the method checks for sons which are zeros, in case she find out, she return false, else she returns true? right ?

Recommended Answers

All 3 Replies

Returns true if each "father" can be divided by each of their "sons". false even if one of the fathers cannot be divided by theirs sons. If a node is null, it does not check it and returns true.

Returns true if each "father" can be divided by each of their "sons". false even if one of the fathers cannot be divided by theirs sons. If a node is null, it does not check it and returns true.

Then if I'll take the tree (attached file) the returns value will be false ?

It will return false. 4 cannot be divided by 8 and 16 cannot be divided by 32

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.