I know how to find the diameter of a BST.
int diameter(struct node * tree) { if (tree == 0) return 0; int lheight = height(tree->left); int rheight = height(tree->right); int ldiameter = diameter(tree->left); int rdiameter = diameter(tree->right); return max(lheight + rheight + 1, max(ldiameter, rdiameter)); } int height(struct node* node) { if(node == NULL) return 0; return 1 + max(height(node->left), height(node->right)); }
What changes should I make to the code to print the path, that is, the nodes corresponding to the diameter of the tree in a sequence from one leaf node, to another leaf node diameter.
For instance: -
8 / \ 1 12 \ / 5 9 / \ 4 7 / 6
output should be 6 7 5 1 8 12 9
source share