Returns the maximum distance between any two nodes in a binary tree and defines the distance between the two nodes.

Source: Internet
Author: User
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?

  1. /*-----------------------------
  2. Copyright by yuucyf. 2011.09.02
  3. ------------------------------*/
  4. # Include "stdafx. h"
  5. # Include
  6. # Include
  7. Using namespace std;
  8. Typedef struct tagSBTreeNode
  9. {
  10. TagSBTreeNode * psLeft;
  11. TagSBTreeNode * psRight;
  12. Int nValue;
  13. Int nMaxLeft;
  14. Int nMaxRight;
  15. TagSBTreeNode ()
  16. {
  17. PsLeft = psRight = NULL;
  18. NValue = 0;
  19. NMaxLeft = nMaxRight = 0;
  20. }
  21. } S_TreeNode;
  22. Void AddTreeNode (S_TreeNode * & psTreeNode, int nValue)
  23. {
  24. If (NULL = psTreeNode)
  25. {
  26. PsTreeNode = new S_TreeNode;
  27. Assert (NULL! = PsTreeNode );
  28. PsTreeNode-> nValue = nValue;
  29. }
  30. Else if (psTreeNode-> nValue <nValue)
  31. {
  32. AddTreeNode (psTreeNode-> psRight, nValue );
  33. }
  34. Else
  35. AddTreeNode (psTreeNode-> psLeft, nValue );
  36. }
  37. Int MaxDepth (const S_TreeNode * psTreeNode)
  38. {
  39. Int nDepth = 0;
  40. If (NULL! = PsTreeNode)
  41. {
  42. Int nLeftDepth = MaxDepth (psTreeNode-> psLeft );
  43. Int nRightDepth = MaxDepth (psTreeNode-> psRight );
  44. NDepth = (nLeftDepth> nRightDepth )? NLeftDepth: nRightDepth;
  45. NDepth ++;
  46. }
  47. Return nDepth;
  48. }
  49. Int MaxDistance (const S_TreeNode * psRootNode)
  50. {
  51. Int nDistance = 0;
  52. If (NULL! = PsRootNode)
  53. {
  54. NDistance = MaxDepth (psRootNode-> psLeft) + MaxDepth (psRootNode-> psRight );
  55. Int nLeftDistance = MaxDistance (psRootNode-> psLeft );
  56. Int nRightDistance = MaxDistance (psRootNode-> psRight );
  57. NDistance = (nLeftDistance> nDistance )? NLeftDistance: nDistance;
  58. NDistance = (nRightDistance> nDistance )? NRightDistance: nDistance;
  59. }
  60. Return nDistance;
  61. }
  62. Int _ tmain (int argc, _ TCHAR * argv [])
  63. {
  64. S_TreeNode * psRoot = NULL;
  65. AddTreeNode (psRoot, 9 );
  66. AddTreeNode (psRoot, 6 );
  67. AddTreeNode (psRoot, 4 );
  68. AddTreeNode (psRoot, 8 );
  69. AddTreeNode (psRoot, 7 );
  70. AddTreeNode (psRoot, 15 );
  71. AddTreeNode (psRoot, 13 );
  72. AddTreeNode (psRoot, 16 );
  73. AddTreeNode (psRoot, 18 );
  74. Cout <"the maximum distance between any two nodes is:" <MaxDistance (psRoot) <endl;
  75. Return 0;
  76. }

/* ----------------------------- 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?

  1. Int g_nMaxLeft = 0;
  2. Void MaxDistance_2 (S_TreeNode * psRoot)
  3. {
  4. // Traverse to the leaf node and return
  5. If (NULL = psRoot)
  6. Return;
  7. // If the left subtree is empty, the maximum left distance of the node is 0.
  8. If (psRoot-> psLeft = NULL)
  9. {
  10. PsRoot-> nMaxLeft = 0;
  11. }
  12. // If the right subtree is empty, the maximum distance to the right of the node is 0.
  13. If (psRoot-> psRight = NULL)
  14. {
  15. PsRoot-> nMaxRight = 0;
  16. }
  17. // If the left subtree is not empty, Recursively search for the longest distance of the Left subtree
  18. If (psRoot-> psLeft! = NULL)
  19. {
  20. MaxDistance_2 (psRoot-> psLeft );
  21. }
  22. // If the right subtree is not empty, Recursively search for the longest distance of the right subtree
  23. If (psRoot-> psRight! = NULL)
  24. {
  25. MaxDistance_2 (psRoot-> psRight );
  26. }
  27. // Calculate the longest node distance of the Left subtree
  28. If (psRoot-> psLeft! = NULL)
  29. {
  30. Int nTempMax = 0;
  31. If (psRoot-> psLeft-> nMaxLeft> psRoot-> psLeft-> nMaxRight)
  32. {
  33. NTempMax = psRoot-> psLeft-> nMaxLeft;
  34. }
  35. Else
  36. {
  37. NTempMax = psRoot-> psLeft-> nMaxRight;
  38. }
  39. PsRoot-> nMaxLeft = nTempMax + 1;
  40. }
  41. // Calculate the longest node distance of the right subtree
  42. If (psRoot-> psRight! = NULL)
  43. {
  44. Int nTempMax = 0;
  45. If (psRoot-> psRight-> nMaxLeft> psRoot-> psRight-> nMaxRight)
  46. {
  47. NTempMax = psRoot-> psRight-> nMaxLeft;
  48. }
  49. Else
  50. {
  51. NTempMax = psRoot-> psRight-> nMaxRight;
  52. }
  53. PsRoot-> nMaxRight = nTempMax + 1;
  54. }
  55. // Update the longest distance
  56. If (psRoot-> nMaxLeft + psRoot-> nMaxRight> g_nMaxLeft)
  57. {
  58. G_nMaxLeft = psRoot-> nMaxLeft + psRoot-> nMaxRight;
  59. }
  60. }

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 ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.