Cell* treeToListHelp(Node* T, Cell* L)
{
if(T == NULL) return L;
else
{
Cell* A = treeToListHelp(T->right, L);
Cell* B = new Cell(T->item, A);
return treeToListHelp(T->left, B);
}
}
Cell* treeToList(Node* T)
{
return treeToListHelp(T, NULL);
}
Remark. If you worked this out using an example, good. If you tried to work this out without looking at an example, what were you thinking?
Suppose that L is list [100, 115] and T is tree
Then treeToListHelp(T, L) should return list [25, 40, 60, 62, 65, 70, 80, 100, 115]. To get that, first compute list A = treeToListHelp(T->right, L). Assuming that treeToListHelp works correctly, you should find that A = [62, 65, 70, 80, 100, 116]. Now create list B by adding just T->item (60) to the front of A. So B = [60, 62, 65, 70, 80, 100, 116]. Finally, compute treeToListHelp(T->left, B), which (assuming treeToListHelp works correctly) should produce list [25, 40, 60, 62, 65, 70, 80, 100, 115].