I. Question:
If a binary tree is regarded as a graph, the line between parent and child nodes is considered as a bidirectional (undirected graph), and "distance" is defined as the number of edges between two nodes. Write a program to find the distance between the two farthest nodes in a binary tree.
Ii. Ideas
Misleading thinking: do not think of the height of the tree.
Correct idea: Find the distance between any two nodes in the graph and the two nodes with the farthest distance.
Solution: A. The distance from the deepest point on the left to the deepest point on the right after passing through the root node.
B. It does not pass through the root node, but the maximum distance between the left or right subtree.
3. Illustration
Case A: Case B:
A
/\/\
B c B o
/\/\/\
D e f g c d
/\
E F
/\
G h
Case A: the maximum distance goes through the vertex D-B-A-C-F (one of them)
Case B: maximum distance without passing through vertex G-E-C-B-D-F-H (one of them)
4. Source Code
# Include "stdio. H "<br/> # include" stdlib. H "<br/> struct node <br/> {<br/> node * pleft; // left subtree <br/> node * pright; // right subtree <br/> int nmaxleft; // The longest distance in the left subtree <br/> int nmaxright; // The longest distance in the right subtree <br/> int chvalue; // the value of this node <br/>}; </P> <p> int nmaxlen = 0; </P> <p> // find the longest two-segment distance in the tree <br/> void findmaxlen (node * proot) <br/>{< br/> // traverse to the leaf node and return <br/> If (proot = NULL) <br/> return; </P> <p> // If the left subtree is empty, the leftmost Long distance is 0 <br/> If (proot-> pleft = NULL) <br/> proot-> nmaxleft = 0; </P> <p> // if the right subtree is empty, the maximum distance to the right of the node is 0 <br/> If (proot-> pright = NULL) <br/> proot-> nmaxright = 0; </P> <p> // If the left subtree is not empty, recursive search for the longest distance left subtree <br/> If (proot-> pleft! = NULL) <br/> findmaxlen (proot-> pleft); </P> <p> // if the right subtree is not empty, recursive search for the longest distance of the right subtree <br/> If (proot-> pright! = NULL) <br/> findmaxlen (proot-> pright ); </P> <p> // calculate the longest node distance of the Left subtree <br/> If (proot-> pleft! = NULL) <br/>{< br/> int ntempmax = 0; <br/> If (proot-> pleft-> nmaxleft> proot-> pleft-> nmaxright) <br/>{< br/> ntempmax = proot-> pleft-> nmaxleft; <br/>}< br/> else <br/>{< br/> ntempmax = proot-> pleft-> nmaxright; <br/>}< br/> proot-> nmaxleft = ntempmax + 1; <br/>}</P> <p> // calculate the longest node distance from the right subtree <br/> If (proot-> pright! = NULL) <br/>{< br/> int ntempmax = 0; <br/> If (proot-> pright-> nmaxleft> proot-> pright-> nmaxright) <br/>{< br/> ntempmax = proot-> pright-> nmaxleft; <br/>}< br/> else <br/>{< br/> ntempmax = proot-> pright-> nmaxright; <br/>}< br/> proot-> nmaxright = ntempmax + 1; <br/>}</P> <p> // update the longest distance <br/> If (proot-> nmaxleft + proot-> nmaxright> nmaxlen) <br/>{< br/> nmaxlen = proot-> nmaxleft + proot-> nmaxright; <br/>}</P> <p> node * createtree () <br/>{< br/> node * root; <br/> int data; <br/> printf ("input data:"); <br/> scanf ("% d", & data ); <br/> // printf ("output data: % d \ n", data); </P> <p> If (Data = 0) <br/> root = NULL; <br/> else/* Create a binary tree in the left and right sides of the root */<br/> {<br/> root = (node *) malloc (sizeof (node); <br/> root-> chvalue = data; <br/> root-> pleft = createtree (); <br/> root-> pright = createtree (); <br/>}< br/> return root; <br/>}< br/> int main () <br/> {<br/> node * root; <br/> root = createtree (); <br/> findmaxlen (Root ); </P> <p> printf ("% d", nmaxlen); <br/> return 0; <br/>}< br/>