1. Problem Description
Write a program to find the distance between the two nodes with the farthest distance between a binary tree
For example:
2. Analysis and Solution
For any node, assume that the root node has k child nodes, the path between the two nearest nodes U and V has two relationships with the root node.
1) if the path goes through Root, U and V belong to different subtree, and they are all the farthest nodes from the subtree to the Root node; otherwise, they are the farthest distance from them.
2) If the paths do not pass through the Root tree, they must belong to one of the Root k Subtrees, and they are also the two farthest vertices in the subtree.
Therefore, the problem can be converted into a word-level solution, so that dynamic planning can be used to solve the problem.
Set the two nodes with the farthest distance in the K subtree: Uk and Vk. The distance is defined as d (Uk, Vk ), the node Uk or Vk is the node with the longest link between the subtree K and the root node Rk. Without interruption, we set Uk as the node with the longest link between the root node and K in the subtree.,The distance to the root node is defined as d (Uk, R ). Take the two largest values max1 and max2 in d (Ui, R) (1 <= I <= k), the maximum path passing through the root node R is max1 + max2 + 2, so the distance between the two points in the tree R is: max {d (U1, V1 ),..., D (Uk, Vk), max1 + max2 + 2 }.
3. Code Implementation
The Code provided by the beauty of programming is as follows:
// Data structure definition struct NODE {NODE * pLeft; // left child NODE * pRight; // right child int nMaxLeft; // The longest distance int nMaxRight in left child; // The longest char chValue of the right child; // value of the NODE}; int nMaxLen = 0; // find the longest two segments in the tree from void FindMaxLen (NODE * pRoot) {// traverse to the leaf node and return if (pRoot = NULL) {return;} // if the left subtree is empty, the longest left distance of the node is 0 if (pRoot-> pLeft = NULL) {pRoot-> nMaxLeft = 0;} // if the right subtree is empty, the maximum distance to the right of the node is 0 if (pRoot-> pRight = NULL) {pRoot-> nMaxRight = 0;} // if the left subtree is not empty, recursive search for the longest distance of the Left subtree If (pRoot-> pLeft! = NULL) {FindMaxLen (pRoot-> pLeft);} // if the right subtree is not empty, Recursively search for the right subtree at the longest distance if (pRoot-> pRight! = NULL) {FindMaxLen (pRoot-> pRight);} if (pRoot-> pLeft! = NULL) {int nTempMax = 0; if (pRoot-> pLeft-> nMaxLeft> pRoot-> pLeft-> nMaxRight) {nTempMax = pRoot-> pLeft-> nMaxLeft ;} else {nTempMax = pRoot-> pLeft-> nMaxRight;} pRoot-> nMaxLeft = nTempMax + 1;} // calculate the longest node distance from the right subtree if (pRoot-> pRight! = NULL) {int nTempMax = 0; if (pRoot-> pRight-> nMaxLeft> pRoot-> pRight-> nMaxRight) {nTempMax = pRoot-> pRight-> nMaxLeft ;} else {nTempMax = pRoot-> pRight-> nMaxRight;} pRoot-> nMaxRight = nTempMax + 1 ;} // update the longest-distance if (pRoot-> nMaxLeft + pRoot-> nMaxRight> nMaxLen) {nMaxLen = pRoot-> nMaxLeft + pRoot-> nMaxRight ;}}
The code for finding the maximum depth based on the binary tree is as follows:
struct BTNode{ int data; BTNode* pLeft; BTNode* pRight;};int maxDis = -1;int findMaxDis(BTNode* pRoot){ if(pRoot == NULL) return 0; int maxLeft = findMaxDis(pRoot->pLeft) ; int maxRight = findMaxDis(pRoot->pRight); if(maxLeft + maxRight > maxDis) { maxDis = maxLeft + maxRight; } return maxLeft > maxRight ? maxLeft+1 : maxRight+1;}
The subsequent code for self-writing does not verify its correctness.