Print Ancestors of a given node in Binary Tree | GeeksforGeeks
Given a Binary Tree and a key, write a function that prints all the ancestors of the key in the given binary tree.
Read full article from Print Ancestors of a given node in Binary Tree | GeeksforGeeks
Given a Binary Tree and a key, write a function that prints all the ancestors of the key in the given binary tree.
/* If target is present in tree, then prints the ancestors and returns true, otherwise returns false. */bool printAncestors(struct node *root, int target){ /* base cases */ if (root == NULL) return false; if (root->data == target) return true; /* If target is present in either left or right subtree of this node, then print this node */ if ( printAncestors(root->left, target) || printAncestors(root->right, target) ) { cout << root->data << " "; return true; } /* Else return false */ return false;}