Describe an algorithm relying only on the BinaryTree operati

Describe an algorithm, relying only on the BinaryTree operations, that counts the number of leaves in a binary tree that their parents are the right child ren of a node.

describe s your algorithm. Pseudo - code is preferred. All details of your algorithm are required to be given

Solution

We create a recursive function:

func rightleaves(node,par)

{

//par variable will be 1 if the parent of the node is right child, it will be 0 if the parent of the node is left and it will -1 for the root node.

if(node->left==NULL && node->right==NULL && par==1)

{return 1;}

a=0;

b=0;

if(node->left!=NULL) { a = rightleaves(node->left,0);}

if(node->right!=NULL) { a = rightleaves(node->right,1);}

return a+b;

}

Describe an algorithm, relying only on the BinaryTree operations, that counts the number of leaves in a binary tree that their parents are the right child ren o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site