1
This function below compiles correctly, works.
int qtd_niveis (tipo_arvore * raiz)
{
if (raiz == NULL)
return 0;
int esquerda = qtd_niveis(raiz -> esq);
int direita = qtd_niveis(raiz -> dir);
if (esquerda > direita)
return ++ esquerda;
return ++ direita;
}
I tried something like this:
int qtd_niveis_esq (tipo_arvore * raiz)
{
if (raiz == NULL)
return 0;
int esquerda = qtd_niveis_esq(raiz -> esq);
if (esquerda > direita)
return ++ esquerda;
}`
Only it does not return me the correct amount of levels left. someone could help me find where the error is. Thank you
Do you want the amount of elements left of the root or do you want to get the length of the leftmost branch of the tree? In the code, apparently you’re doing the second, but something tells me you want the first.
– Woss
I want to know only how many levels has left, the first code returns how many levels has a tree, if it has 6 left and 7 right, will return me 7, that in the first code, and in the second code, have to return me for example only 6.
– André
Just so we’re clear on this one image, what elements should be considered?
FBA
because it is the leftmost branch, orFBDC
for being the largest to the left of the root?– Woss
Elements:
ABCDEF
(em_order )left, has to return 4 levels. If the right side had more levels, for example 5 levels, I would not be interested. I only care left which is 4 levels.– André
Something thus works?
– Woss
Here returned
7
instead of4
– André
I think this code of yours is counting all levels on each side, being in my example
4
left and3
right, so7
– André
Let’s go continue this discussion in chat.
– Woss