•      Sign In
• Create Account

### #ActualCornstalks

Posted 30 May 2012 - 09:17 AM

In the first one, the variable leaf is unnecessary. It is never used, and can be taken out. Other than that, it looks correct.

The second one is not quite correct, however. There are two problems. First, a leaf is a node where both children are NULL (that is, it has no children). If a node has one or more children, it is not a leaf. Your if statements ((node->getFirstLink() != NULL && node->getLastLink() == NULL) and (node->getFirstLink() == NULL && node->getLastLink() != NULL)) check if the node has one child. They do not check if it has no children (which would be the proper check). You should check if both children are NULL, and if so return 1. You should not have two if statements either.

The second problem is the last return value, return (1 + x + y). Why is that 1 there? If you reach this statement, it's clear the node is not a leaf, and so should not return (1 + x + y), but instead should just return x + y.

Whenever I write an algorithm or data structure, I like to verify it on paper. So in your case, I would draw a binary tree on some paper, and then "run" the function by hand and see if I got the right answer.

   A
/   \
B    C
/ \   /
D E  F


Try running your function on that tree (by hand!) and see if you get the correct answer of 3.

### #4Cornstalks

Posted 30 May 2012 - 09:17 AM

In the first one, the variable leaf is unnecessary. It is never used, and can be taken out. Other than that, it looks correct.

The second one is not quite correct, however. There are two problems. First, a leaf is a node where both children are NULL (that is, it has no children). If a node has one or more children, it is not a leaf. Your if statements ((node->getFirstLink() != NULL && node->getLastLink() == NULL) and (node->getFirstLink() == NULL && node->getLastLink() != NULL)) check if the node has one child. They do not check if it has no children (which would be the proper check). You should check if both children are NULL, and if so return 1. You should not have two if statements either.

The second problem is the last return value, return (1 + x + y). Why is that 1 there? If you reach this statement, it's clear the node is not a leaf, and so should not return (1 + x + y), but instead should just return x + y.

Whenever I write an algorithm or data structure, I like to verify it on paper. So in your case, I would draw a binary tree on some paper, and then "run" the function by hand and see if I got the right answer.

   A
/  \
B   C
/ \  /
D E F


Try running your function on that tree (by hand!) and see if you get the correct answer of 3.

### #3Cornstalks

Posted 30 May 2012 - 09:17 AM

In the first one, the variable leaf is unnecessary. It is never used, and can be taken out. Other than that, it looks correct.

The second one is not quite correct, however. There are two problems. First, a leaf is a node where both children are NULL (that is, it has no children). If a node has one or more children, it is not a leaf. Your if statements ((node->getFirstLink() != NULL && node->getLastLink() == NULL) and (node->getFirstLink() == NULL && node->getLastLink() != NULL)) check if the node has one child. They do not check if it has no children (which would be the proper check). You should check if both children are NULL, and if so return 1. You should not have two if statements either.

The second problem is the last return value, return (1 + x + y). Why is that 1 there? If you reach this statement, it's clear the node is not a leaf, and so should not return (1 + x + y), but instead should just return x + y.

Whenever I write an algorithm or data structure, I like to verify it on paper. So in your case, I would draw a binary tree on some paper, and then "run" the function by hand and see if I got the right answer.

  A
/  \
B   C
/ \  /
D E F


Try running your function on that tree (by hand!) and see if you get the correct answer of 3.

### #2Cornstalks

Posted 30 May 2012 - 09:16 AM

In the first one, the variable leaf is unnecessary. It is never used, and can be taken out. Other than that, it looks correct.

The second one is not quite correct, however. There are two problems. First, a leaf is a node where both children are NULL (that is, it has no children). If a node has one or more children, it is not a leaf. Your if statements ((node->getFirstLink() != NULL && node->getLastLink() == NULL) and (node->getFirstLink() == NULL && node->getLastLink() != NULL)) check if the node has one child. They do not check if it has no children (which would be the proper check). You should check if both children are NULL, and if so return 1. You should not have two if statements either.

The second problem is the last return value, return (1 + x + y). Why is that 1 there? If you reach this statement, it's clear the node is not a leaf, and so should not return (1 + x + y), but instead should just return x + y.

Whenever I write an algorithm or data structure, I like to verify it on paper. So in your case, I would draw a binary tree on some paper, and then "run" the function by hand and see if I got the right answer.

  A
/  \
B   C
/ \  /
D E F


Try running your function on that tree (by hand!) and see if you get the correct answer of 3.

### #1Cornstalks

Posted 30 May 2012 - 09:16 AM

In the first one, the variable leaf is unnecessary. It is never used, and can be taken out. Other than that, it looks correct.

The second one is not quite correct, however. There are two problems. First, a leaf is a node where both children are NULL (that is, it has no children). If a node has one or more children, it is not a leaf. Your if statements ((node->getFirstLink() != NULL && node->getLastLink() == NULL) and (node->getFirstLink() == NULL && node->getLastLink() != NULL)) check if the node has one child. They do not check if it has no children (which would be the proper check). You should check if both children are NULL, and if so return 1. You should not have two if statements either.

The second problem is the last return value, return (1 + x + y). Why is that 1 there? If you reach this statement, it's clear the node is not a leaf, and so should not return (1 + x + y), but instead should just return x + y.

Whenever I write an algorithm or data structure, I like to verify it on paper. So in your case, I would draw a binary tree on some paper, and then "run" the function by hand and see if I got the right answer.

  A
/  \
B  C
/ \  /
D E F


Try running your function on that tree (by hand!) and see if you get the correct answer of 3.

PARTNERS