![Quote](https://cboard.cprogramming.com/images/misc/quote_icon.png)
Originally Posted by
kmdv
If you want to get the number of parents, you need a parent counter for each node, and then:
1. Reset counter for every node to 0.
2. Recursively visit nodes. For each visited node, increment its counter. If the counter reaches 1, continue visiting its children, otherwise return.
Each node will hold the number of parent nodes (assuming that there are no cycles).
Hi thanks, is it achievable with 1 function?
the code i came up with doesnt seems to go over 1 ever
Code:
bool checknode(CPPtr SP){ bool chkl = false;
bool chkr = false;
bool chk = false;
if(SP==NULL){
return false;
}
else {
chkl = checknode(SP->left);
chkr = checknode(SP->right);
SP->count++;
if(SP->count>1){
return true; //return true if a node is called twice
}
if(chkl==true||chkr==true){
chk = true;
}
}
return chk;
}