Topic:
Consider the two-fork tree as a graph, the connection between the parent and child nodes is bidirectional, and the distance is defined as the number of sides between the two nodes.
The maximum value of the distance between two nodes in a binary tree.
Method One: Use the book to write the method:
Code:
struct Node {node *left; Node *right; int nmaxleft; int nmaxright; char ch;}; int nmaxlength = 0;void findmaxdistance (node *root) {if (root = NULL) return; if (Root->left = = NULL) root->nmaxleft = 0; if (root->right = = NULL) root->nmaxright = 0; if (root->left! = NULL) findmaxdistance (root->left); if (root->right! = NULL) findmaxdistance (root->right); if (root->left! = NULL) {int temp = 0; if (Root->left->nmaxleft > Root->left->nmaxright) temp = root->left->nmaxleft; else temp = root->left->nmaxright; Root->nmaxleft = temp + 1; } if (root->right! = NULL) {int temp = 0; if (Root->right->nmaxleft > Root->right->nmaxright) temp = root->right->nmaxleft; else temp = root->right->nmaxright; Root->nmaxright = temp + 1; } if (Root->nmaxleft + root->nmaxright > nMaxLength) nmaxlength = Root->nmaxleft + Root->nmaxright;}
Method Two:
definition : In a subtree of node x as the root node, the maximum distance between nodes is dis (x).
Two points of maximum distance may appear in three cases when the maximum distance between points x is obtained
- Zuozi
- Right sub-tree
- Feast point X
After analysis, the following characteristics are obtained
- The above three cases must end with a leaf
- In the third case, it must be the sum of the height of the Zuozi height and the right subtree (only in this way can we get the maximum value)
After the above analysis, we can get the recursive type
Dis (x) = max (DIS (x->left), Dis (x->right), height (x->left) +height (x->right))
Code:
struct Node { node *left; Node *right; char ch;}; int maxLength = 0;int Height (node *root) { if (root = NULL) return 0; Return Height (Root->left) > Height (root->right)? Height (Root->left) +1:height (root->right) +1;} int findmaxdistance (node *root) { if (root = NULL) return 0; else if (Root->left = = NULL && Root->right = = null) return 0; int dis = max (findmaxdistance (Root->left), Findmaxdistance (root->right), Height (root->left) +height ( Root->right)); if (Dis > maxLength) maxLength = dis; return dis;}
Method Three:
Idea: Using depth-first traversal method.
These two points are bound to be in a node A as the root of the subtree, the path between them must pass through the root node of that subtree A .
Thus, a subtree with any node as its root calculates the maximum distance through that subtree node, and the maximum distance is the maximum distance required.
and the maximum distance through the root node of a tree = the height of the Zuozi + height of right sub-tree +2 (assuming that the height of the empty node is -1 ), so you can use a global variable Max_d Save the maximum distance, take the depth first traversal, each traverse a node, calculate the height of the left and right subtree, calculate its height, and will pass through the node's maximum distance value and Max_d value comparison, and update Max_d ,
when all nodes are traversed, max_d is the maximum distance to be asked.
Code:
int maxlen = 0;int findmaxdistance (node *root, int &maxlen) { //Every child node encountered, height increased by 1, can be set empty node height is-1, // Avoid the judgment of empty nodes when calculating heights. if (root = = NULL) return-1; int leftlen = Findmaxdistance (Root->left, maxlen) + 1; int rightlen = Findmaxdistance (Root->right, maxlen) + 1; if (Leftlen + Rightlen > MaxLen) maxlen = Leftlen + Rightlen; return Leftlen > Rightlen? Leftlen:rightlen;}
3.8 Finding the maximum distance of a node in a binary tree