Question: calculate the maximum distance between any two nodes in a binary tree. The distance between the two nodes is defined as the number of edges between the two nodes, for example, the distance between a child node and its parent node is 1, and the distance between the child node and the adjacent brother node is 2. This optimizes the time space clutter. Idea 1: There are two situations for calculating the maximum distance of A binary tree: Case A: the path goes through the left sub-tree
Question: calculate the maximum distance between any two nodes in a binary tree. The distance between the two nodes is defined as the number of edges between the two nodes, for example, the distance between a child node and its parent node is 1, and the distance between the child node and the adjacent brother node is 2. This optimizes the time space clutter. Idea 1: There are two situations for calculating the maximum distance of A binary tree: Case A: the path goes through the left sub-tree
Question:
Calculate the maximum distance between any two nodes in a binary tree. The distance between the two nodes is defined as the number of edges between the two nodes. For example, the distance between a child node and its parent node is 1, the distance between the two adjacent sibling nodes is 2,
Optimized Time Space clutter.
Thought 1:
There are two possible conditions for calculating the maximum distance of a binary tree:
Case A: the path goes through the deepest node of the Left subtree, and then to the deepest node of the right subtree through the root node.
Case B: the path does not pass through the root node, but is the maximum distance path of the Left or right subtree.
First, calculate the distance of the maximum path passing through the root node, which is actually the depth and distance of the left and right subtree, and then calculate the maximum distance between the left and right subtree, respectively, the maximum value is the maximum distance of the current binary tree.
The Code is as follows:
[Cpp]View plaincopyprint?
- /*-----------------------------
- Copyright by yuucyf. 2011.09.02
- ------------------------------*/
- # Include "stdafx. h"
- # Include
- # Include
- Using namespace std;
-
- Typedef struct tagSBTreeNode
- {
- TagSBTreeNode * psLeft;
- TagSBTreeNode * psRight;
- Int nValue;
-
- Int nMaxLeft;
- Int nMaxRight;
-
- TagSBTreeNode ()
- {
- PsLeft = psRight = NULL;
- NValue = 0;
- NMaxLeft = nMaxRight = 0;
- }
- } S_TreeNode;
-
-
- Void AddTreeNode (S_TreeNode * & psTreeNode, int nValue)
- {
- If (NULL = psTreeNode)
- {
- PsTreeNode = new S_TreeNode;
- Assert (NULL! = PsTreeNode );
- PsTreeNode-> nValue = nValue;
- }
- Else if (psTreeNode-> nValue <nValue)
- {
- AddTreeNode (psTreeNode-> psRight, nValue );
- }
- Else
- AddTreeNode (psTreeNode-> psLeft, nValue );
- }
-
- Int MaxDepth (const S_TreeNode * psTreeNode)
- {
- Int nDepth = 0;
- If (NULL! = PsTreeNode)
- {
- Int nLeftDepth = MaxDepth (psTreeNode-> psLeft );
- Int nRightDepth = MaxDepth (psTreeNode-> psRight );
- NDepth = (nLeftDepth> nRightDepth )? NLeftDepth: nRightDepth;
- NDepth ++;
- }
-
- Return nDepth;
- }
-
- Int MaxDistance (const S_TreeNode * psRootNode)
- {
- Int nDistance = 0;
- If (NULL! = PsRootNode)
- {
- NDistance = MaxDepth (psRootNode-> psLeft) + MaxDepth (psRootNode-> psRight );
- Int nLeftDistance = MaxDistance (psRootNode-> psLeft );
- Int nRightDistance = MaxDistance (psRootNode-> psRight );
-
- NDistance = (nLeftDistance> nDistance )? NLeftDistance: nDistance;
- NDistance = (nRightDistance> nDistance )? NRightDistance: nDistance;
- }
-
- Return nDistance;
- }
-
-
-
-
- Int _ tmain (int argc, _ TCHAR * argv [])
- {
- S_TreeNode * psRoot = NULL;
- AddTreeNode (psRoot, 9 );
- AddTreeNode (psRoot, 6 );
- AddTreeNode (psRoot, 4 );
- AddTreeNode (psRoot, 8 );
- AddTreeNode (psRoot, 7 );
- AddTreeNode (psRoot, 15 );
- AddTreeNode (psRoot, 13 );
- AddTreeNode (psRoot, 16 );
- AddTreeNode (psRoot, 18 );
-
- Cout <"the maximum distance between any two nodes is:" <MaxDistance (psRoot) <endl;
-
- Return 0;
- }
/* ----------------------------- Copyright by yuucyf. 2011.09.02 -------------------------------- */# include "stdafx. h" # include
# Include using namespace std; typedef struct tagSBTreeNode {tagSBTreeNode * psLeft; tagSBTreeNode * psRight; intnValue; int nMaxLeft; int nMaxRight; weight () {psLeft = psRight = NULL; nValue = 0; nMaxLeft = nMaxRight = 0 ;}s_treenode; void AddTreeNode (S_TreeNode * & psTreeNode, int nValue) {if (NULL = psTreeNode) {psTreeNode = new S_TreeNode; assert (NULL! = PsTreeNode); psTreeNode-> nValue = nValue;} else if (psTreeNode-> nValue <nValue) {AddTreeNode (psTreeNode-> psRight, nValue );} elseAddTreeNode (psTreeNode-> psLeft, nValue);} int MaxDepth (const S_TreeNode * psTreeNode) {int nDepth = 0; if (NULL! = PsTreeNode) {int nLeftDepth = MaxDepth (psTreeNode-> psLeft); int nRightDepth = MaxDepth (psTreeNode-> psRight); nDepth = (nLeftDepth> nRightDepth )? NLeftDepth: nRightDepth; nDepth ++;} return nDepth;} int MaxDistance (const S_TreeNode * psRootNode) {int nDistance = 0; if (NULL! = PsRootNode) {nDistance = MaxDepth (psRootNode-> psLeft) + MaxDepth (psRootNode-> psRight); int nLeftDistance = MaxDistance (psRootNode-> psLeft ); int nRightDistance = MaxDistance (psRootNode-> psRight); nDistance = (nLeftDistance> nDistance )? NLeftDistance: nDistance; nDistance = (nRightDistance> nDistance )? NRightDistance: nDistance;} return nDistance;} int _ tmain (int argc, _ TCHAR * argv []) {S_TreeNode * psRoot = NULL; AddTreeNode (psRoot, 9 ); addTreeNode (psRoot, 6); AddTreeNode (psRoot, 4); AddTreeNode (psRoot, 8); AddTreeNode (psRoot, 7); AddTreeNode (psRoot, 15); AddTreeNode (psRoot, 13); AddTreeNode (psRoot, 16); AddTreeNode (psRoot, 18); cout <"the maximum distance between any two nodes is:" <MaxDistance (psRoot) <endl; return 0 ;}
Thought 2:
Idea 1 is not the most efficient, because there are repeated computations when calculating the depth of Binary Trees. However, it should be readable, without changing the structure of the original binary tree and using additional global variables. The code is provided here, because the comments of the Code have been written in great detail.
The Code is as follows:
[Cpp]View plaincopyprint?
- Int g_nMaxLeft = 0;
- Void MaxDistance_2 (S_TreeNode * psRoot)
- {
- // Traverse to the leaf node and return
- If (NULL = psRoot)
- Return;
-
- // If the left subtree is empty, the maximum left distance of the node is 0.
- If (psRoot-> psLeft = NULL)
- {
- PsRoot-> nMaxLeft = 0;
- }
-
- // If the right subtree is empty, the maximum distance to the right of the node is 0.
- If (psRoot-> psRight = NULL)
- {
- PsRoot-> nMaxRight = 0;
- }
-
- // If the left subtree is not empty, Recursively search for the longest distance of the Left subtree
- If (psRoot-> psLeft! = NULL)
- {
- MaxDistance_2 (psRoot-> psLeft );
- }
-
- // If the right subtree is not empty, Recursively search for the longest distance of the right subtree
- If (psRoot-> psRight! = NULL)
- {
- MaxDistance_2 (psRoot-> psRight );
- }
-
- // Calculate the longest node distance of the Left subtree
- If (psRoot-> psLeft! = NULL)
- {
- Int nTempMax = 0;
- If (psRoot-> psLeft-> nMaxLeft> psRoot-> psLeft-> nMaxRight)
- {
- NTempMax = psRoot-> psLeft-> nMaxLeft;
- }
- Else
- {
- NTempMax = psRoot-> psLeft-> nMaxRight;
- }
- PsRoot-> nMaxLeft = nTempMax + 1;
- }
-
- // Calculate the longest node distance of the right subtree
- If (psRoot-> psRight! = NULL)
- {
- Int nTempMax = 0;
- If (psRoot-> psRight-> nMaxLeft> psRoot-> psRight-> nMaxRight)
- {
- NTempMax = psRoot-> psRight-> nMaxLeft;
- }
- Else
- {
- NTempMax = psRoot-> psRight-> nMaxRight;
- }
- PsRoot-> nMaxRight = nTempMax + 1;
- }
-
- // Update the longest distance
- If (psRoot-> nMaxLeft + psRoot-> nMaxRight> g_nMaxLeft)
- {
- G_nMaxLeft = psRoot-> nMaxLeft + psRoot-> nMaxRight;
- }
- }
Int g_nMaxLeft = 0; void MaxDistance_2 (S_TreeNode * psRoot) {// traverse to the leaf node and return if (NULL = psRoot) return; // if the left subtree is empty, the longest left distance of the node is 0if (psRoot-> psLeft = NULL) {psRoot-> nMaxLeft = 0;} // if the right subtree is empty, the maximum distance to the right of the node is 0if (psRoot-> psRight = NULL) {psRoot-> nMaxRight = 0;} // If the left subtree is not empty, recursively search for the longest left subtree if (psRoot-> psLeft! = NULL) {MaxDistance_2 (psRoot-> psLeft);} // if the right subtree is not empty, Recursively search for the longest distance of the right subtree if (psRoot-> psRight! = NULL) {MaxDistance_2 (psRoot-> psRight);} // calculates the longest node distance from the left subtree to if (psRoot-> psLeft! = NULL) {int nTempMax = 0; if (psRoot-> psLeft-> nMaxLeft> psRoot-> psLeft-> nMaxRight) {nTempMax = psRoot-> psLeft-> nMaxLeft ;} else {nTempMax = psRoot-> psLeft-> nMaxRight;} psRoot-> nMaxLeft = nTempMax + 1;} // calculates the longest node distance from the right subtree if (psRoot-> psRight! = NULL) {int nTempMax = 0; if (psRoot-> psRight-> nMaxLeft> psRoot-> psRight-> nMaxRight) {nTempMax = psRoot-> psRight-> nMaxLeft ;} else {nTempMax = psRoot-> psRight-> nMaxRight;} psRoot-> nMaxRight = nTempMax + 1 ;} // update the longest-distance if (psRoot-> nMaxLeft + psRoot-> nMaxRight> g_nMaxLeft) {g_nMaxLeft = psRoot-> nMaxLeft + psRoot-> nMaxRight ;}}