[Learn Little Bit-Data Structure-binary tree] calculates the maximum distance between two nodes of a binary tree.

Source: Internet
Author: User
/** 1. programming method. */Struct node {node * pleft; // left subtree node * pright; // right subtree int nmaxleft; // The longest distance int nmaxright in the left subtree; // The longest-distance char chvalue in the right subtree; // 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 maximum left distance of the node is 0 if (proot-> pleft = NULL) {proot-> nmaxleft = 0 ;} // if the right subtree is empty, the maximum right distance of the node is 0 if (proot-> pright = NULL) {proot-> nmaxright = 0 ;} // If the left subtree is not empty, Recursively search for the longest distance 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);} // calculates the longest node distance from the left subtree to 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 ;}}

// Other solutions 2 # include <stdio. h> # include <stdlib. h> # include <malloc. h> # define leaf-1 typedef struct btreenode {btreenode * lchild; btreenode * rchild; int value;} btreenode, * btree; btreenode * createtree () {btreenode * t; int t; scanf ("% d", & T); If (t = leaf) {T = NULL;} else {T = (btreenode *) malloc (sizeof (btreenode); t-> value = T; t-> lchild = createtree (); t-> rchild = createtree ();} return t ;} int max (int, Int B) {return A> B? A: B;} int findmaxlen (btreenode * root, Int & depth) {If (root = NULL) {depth = 0; return 0;} int leftlen = 0, rightlen = 0, maxleft = 0, maxright = 0; If (root-> lchild! = NULL) {maxleft = findmaxlen (root-> lchild, leftlen);} If (root-> rchild! = NULL) {maxright = findmaxlen (root-> rchild, rightlen);} depth = max (leftlen, rightlen) + 1; return max (maxleft, max (maxright, leftlen + rightlen);} Main () {btreenode * root; root = createtree (); int depth = 0, maxlen = 0; maxlen = findmaxlen (root, depth ); printf ("% d \ n", maxlen); System ("pause"); Return 0 ;}

// Reference: Using namespace STD; struct node {node * pleft; node * pright;}; struct result {int nmaxdistance; int nmaxdepth;}; Result getmaximumdistance (node * root) {If (! Root) {result empty = {0,-1}; // TRICK: nmaxdepth is-1 and then caller will plus 1 to balance it as zero. return empty;} result LHS = getmaximumdistance (root-> pleft); Result RHS = getmaximumdistance (root-> pright); Result result; result. nmaxdepth = max (LHS. nmaxdepth + 1, RHS. nmaxdepth + 1); result. nmaxdistance = max (LHS. nmaxdistance, RHS. nmaxdistance), LHS. nmaxdepth + RHS. nmaxdepth + 2); return result ;}

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.