<> Find the length of the first longest path and output the path
* Algorithmic thought
Using the idea of recursive traversal , First, find the longest path of the binary tree, that is, the height of the current tree
Then call the function to find the path length to judge the direction of the pointer to the output path int LongestPath(BiTree *T){ // Find the longest path of the current binary tree if(T==0)
return 0; if(T->lchild==0&&T->rchild==0) return 1; int L=LongestPath(T->lchild);
int R=LongestPath(T->rchild); return (L>R?L:R)+1; } void Long(BiTree *T){
// Output path function if(T==0) return; printf(" %c",T->data); // Equivalent to preamble int L=LongestPath(T->
lchild);// Calculate the height of the left subtree int R=LongestPath(T->rchild);// Calculate the height of the right subtree if(L>=R) Long(T->
lchild);// The direction is determined according to the height of the left and right subtrees else Long(T->rchild); }
* Operation results
Technology
Daily Recommendation