Consider the following C program segment
struct CellNode{ struct CellNode *leftChild int element; struct CellNode *rightChild; }; int Dosomething (struct CellNode *ptr) { int value = 0; if(ptr != NULL) { if (ptr -> leftChild != NULL) value = 1 + DoSomething (ptr -> leftChild); if (ptr -> rightChild != NULL) value = max(value, 1 + Dosomething (ptr -> rightChild)); } return(value); }
The value returned by the function $\text{DoSomething}$ when a pointer to the root of a non-empty tree is passed as argument is
1st if calculates the height of the left subtree and 2nd if computes the height of the right subtree and takes the max of (height of left subtree, height of right subtree).
(D) It calculates Height of tree. Easy way to get this answer . Draw a tree where all $4$ parameters are different. Get a Tree for which Height, No of Internal Nodes & No of Leafs are different & Trace out this algorithm.
The below diagram may make the code easier to understand.
How I did that: I drew a tree then just tried the algo on this tree and then I modified the tree wisely, then I tried the algo one more time
what kind of explanation is this? :D :D
Feedback for next edition (if ever there's...