Calculate the maximum distance between nodes in a binary tree.

Source: Internet
Author: User

The beauty of programming provides a method, but it stores a field with the longest left subtree distance and the longest right subtree distance for each node.

I think of another method: node encoding. The root node is set to 0. For the left node of a node, its code is followed by the Father's Day Code. For the right node of a node, its code is followed by the father's day code, if we find the leaf node, we will put the code for it in an array. After a traversal, all leaf nodes are encoded in the array, and the Root Node Code is also included in the array. We can find the distance between two nodes based on the Node Code. Because the two nodes with the maximum distance must be leaf nodes, leaf nodes, and root nodes, you only need to compare the distance between codes in the array to obtain the maximum distance.

 

Khan, every time I talk about my thoughts, I am confused... In fact, as long as you draw a secondary image, you can draw it yourself...

 

# Include <iostream> <br/> # include <vector> <br/> # include <string> <br/> using namespace STD; </P> <p> template <class T> <br/> struct btnode <br/> {<br/> T data; <br/> btnode <t> * left; <br/> btnode <t> * right; <br/> btnode (t I = 0, btnode <t> * l = NULL, btnode <t> * r = NULL): Data (I), left (L), right (r ){}; <br/>}; </P> <p> vector <string> code; </P> <p> // indicates the leaf node encoding, the serial number is stored in the array code. </P> <p> template <class T> <br/> void code (btnode <t> * P, string S) <br/>{< br/> If (P = NULL) <br/> return; <br/> If (p-> left! = NULL) <br/> code (p-> left, S + "0"); <br/> If (p-> right! = NULL) <br/> code (p-> right, S + "1 "); <br/> If (p-> left = NULL & P-> right = NULL) // leaf node <br/> code. push_back (s); <br/>}</P> <p> template <class T> <br/> int maxdistance (btnode <t> * root) <br/>{< br/> If (root = NULL) <br/> return 0; <br/> code. push_back ("0"); <br/> code (root, "0"); // Code <br/> int max = 0; <br/> int I, j; <br/> string S1, S2; <br/> for (I = 0; I <code. size (); I ++) <br/>{< br/> for (j = I; j <code. size (); j ++) <br/> {<br/> S1 = Code [I]; <br/> S2 = Code [J]; <br/> int K = 0; <br/> while (k <s1.length () & K <s2.length () & s1.at (K) = s2.at (k) // calculate the distance based on the encoding code. <Br/> K ++; <br/> int temp = (s1.length ()-k) + (s2.length ()-K )); <br/> If (temp> MAX) <br/> max = temp; <br/>}</P> <p> return Max; <br/>}</P> <p> int main () <br/>{< br/> btnode <char> * H = new btnode <char> ('H'); // use a template, note new <br/> btnode <char> * I = new btnode <char> ('I '); <br/> btnode <char> * D = new btnode <char> ('D', H ); <br/> btnode <char> * E = new btnode <char> ('E '); <br/> btnode <char> * B = new btnode <char> ('B', d, e ); <br/> btnode <char> * f = new btnode <char> ('F', null, I ); <br/> btnode <char> * g = new btnode <char> ('G '); <br/> btnode <char> * c = new btnode <char> ('C', f, g ); <br/> btnode <char> * A = new btnode <char> ('A', B, c); </P> <p> cout <maxdistance () <Endl; <br/> while (1); <br/> return 0; <br/>}

 

Attach another solution in the beauty of Programming

 

# Include <iostream> <br/> using namespace STD; </P> <p> # define max (A, B) (a)> (B )? (A) :( B) </P> <p> struct node <br/> {<br/> node * left; <br/> node * right; <br/> int nmaxleft; // The longest distance in the left subtree <br/> int nmaxright; // The longest distance in the right subtree <br/> char chvalue; <br/> node (char a, node * l = NULL, node * r = NULL): chvalue (A), left (L), right (r) {}< br/>}; </P> <p> int nmaxlen = 0; </P> <p> void findmaxlen (node * root) <br/>{< br/> If (root = NULL) <br/> return; <br/> If (root-> left = NULL) <br/> root-> nmaxleft = 0; <br/> If (root-> right = NUL L) <br/> root-> nmaxright = 0; <br/> If (root-> left! = NULL) <br/> findmaxlen (root-> left); <br/> If (root-> right! = NULL) <br/> findmaxlen (root-> right); <br/> If (root-> left! = NULL) <br/> root-> nmaxleft = max (root-> left-> nmaxleft, root-> left-> nmaxright) + 1; <br/> If (root-> right! = NULL) <br/> root-> nmaxright = max (root-> right-> nmaxleft, root-> right-> nmaxright) + 1; <br/> If (root-> nmaxleft + root-> nmaxright> nmaxlen) <br/> nmaxlen = root-> nmaxleft + root-> nmaxright; <br/>}</P> <p> int main () <br/> {<br/> node * H = new node ('H '); <br/> node * I = new node ('I'); <br/> node * E = new node ('E', H ); <br/> node * c = new node ('C', e); <br/> node * f = new node ('F', null, I ); <br/> node * g = new node ('G'); <br/> node * D = new node ('D', f, g ); <br/> node * B = new node ('B', c, d); <br/> node * A = new node ('A', null, B ); </P> <p> findmaxlen (a); <br/> cout <nmaxlen <Endl; <br/> while (1); <br/>}

 

 

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.