Data structure and algorithm surface test questions 80 (11)

Source: Internet
Author: User

11. Find the maximum distance of the nodes in the binary tree ...

If we look at the two-fork tree as a graph, the connection between the parent and child nodes is considered bidirectional,

Let's define the number of edges between two nodes for distance.

Write a program,

Find the distance between the two nodes farthest apart in a binary tree.

Wrong thought: Find the deepest nodes of the left and right subtree to the root, add together.

Cause of error: The deepest nodes of the left and right subtree on the left dial hand tree to the root are added more than the deepest nodes of the root node and the left and right sub-tree to the root.

Correct thought: Use dynamic programming to record the maximum distance between the children and the nodes.

Correct reason: From the wrong reason can know, the maximum distance for a certain node to it about the maximum distance of children and

Problem Solving Method: (1) A certain knot, left child is empty, it to the left child's distance is 0, the right child is empty, it to the right child's distance is 0;

(2) If the left child is not empty, the maximum distance of the left subtree of the node is: the maximum distance of the sub-tree is +1; if the right child is not empty, the maximum distance of the right subtree of the node is: the maximum distance of the subtree is +1;

(3) Maintain maximum distance. Set a global variable to record the maximum distance and the maximum value of the child's left and right children.

(4) results. The result is equal to the maximum distance.

#include <cstdio>#include<iostream>using namespacestd;structbstreenode{intMmaxleft;//The longest distance to the left child    intMmaxright;//The longest distance to the right child    intM_nvalue;//Value of NodeBstreenode *m_pleft;//Left child nodeBstreenode *m_pright;//Right Child node};//set up a two-fork sorting treevoidAddbstreenode (Bstreenode *&pcurrent,intvalue) {    //If the current node is empty, insert it directly, if it is larger than the current node, insert the current knot.//the right node of the point, if Next, inserts the left node. If equal, the value is ignored by default//the & of the first parameter is very important. Otherwise you need to manually traverse the tree to find the location of the node.    if(pcurrent==NULL) {Bstreenode*pbstree=NewBstreenode (); Pbstree->mmaxleft=0; Pbstree->mmaxright=0; Pbstree->m_nvalue=value; Pbstree->m_pleft=NULL; Pbstree->m_pright=NULL; Pcurrent=Pbstree; }Else if(pcurrent->m_nvalue>value) {Addbstreenode (pcurrent-m_pleft,value); }Else if(pcurrent->m_nvalue<value) {Addbstreenode (pcurrent-m_pright,value); }Else{cout<<"node Repeated,default ignore"<<Endl; }}intmmaxlen=0;//Maximum Value//through the root node, the maximum distance is ansvoidMax_distant (Bstreenode *root) {    if(Root==null)return;//traverse to the leaf node and return    if(root->m_pright==null) root->mmaxright=0; if(root->m_pleft==null) root->mmaxleft=0; if(Root->m_pright!=null)//If the right subtree is empty, the maximum length is 0, not null, recursiveMax_distant (root->m_pright); if(Root->m_pleft!=null)//If the left dial hand tree is empty, the maximum length is 0, not null, recursiveMax_distant (root->m_pleft); if(Root->m_pleft!=null) {//Calculate Zuozi maximum node distance        intmtempmax=0;//Temporary Maximum value        if(root->m_pleft->mmaxleft>root->m_pleft->mmaxright) Mtempmax=root->m_pleft->Mmaxleft; ElseMtempmax=root->m_pleft->Mmaxright; Root->mmaxleft=mtempmax+1;//Update Maximum Value    }    if(Root->m_pright!=null) {//calculate the longest node distance of the right subtree        intmtempmax=0;//Temporary Maximum value        if(root->m_pright->mmaxleft>root->m_pright->mmaxright) Mtempmax=root->m_pright->Mmaxleft; ElseMtempmax=root->m_pright->Mmaxright; Root->mmaxright=mtempmax+1;//Update Maximum Value    }    if(root->mmaxleft+root->mmaxright>Mmaxlen) Mmaxlen=root->mmaxleft+root->mmaxright;}intMain () {Bstreenode*root=NULL; Addbstreenode (Root,Ten); Addbstreenode (Root,6); Addbstreenode (Root, -); Addbstreenode (Root,5); Addbstreenode (Root, One); Addbstreenode (Root,3);    Max_distant (root); cout<< (mmaxlen<<1) <<Endl;}

Data structure and algorithm surface test questions 80 (11)

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.