13. design an algorithm to find the nearest common parent node of any two nodes on the binary tree.

Source: Internet
Author: User
/*************************************** * ******************************** // 13. Design algorithm, find the nearest common parent node of any two nodes on the binary tree. If the complexity is O (n2), no score is obtained. *//************************************* ********************************** // There is a solution for offer, the sub-node has a pointer to the parent node. // each node to the root node is a linked list, and the first public node from two nodes to the root node is obtained, // The child node does not point to the Father's Day node, however, the path of node A and Node B is recorded in the stack // The path patha from root to a is recorded in the closest co-parent node of Node B, record the path from root to B. pathb // compare the length difference between patha and pathb. The long path first has D nodes, and then patha and pathb both go out of the stack, find the First Equal node/* elements in the stack and copy them to the list */void stacktolist (stack <binarynode *> nodestack, list <binarynode *> & path1) {While (! Nodestack. empty () {path1.push _ back (nodestack. top (); nodestack. pop () ;}/// obtain the path bool g_isfind = false in subsequent traversal; void postorderfindpath (binarynode * root, binarynode * LHS, list <binarynode *> & Path) {If (g_isfind = false & root) {path. push_front (Root); postorderfindpath (root-> m_left, LHS, PATH); postorderfindpath (root-> m_right, LHS, PATH); If (root = LHS) {g_isfind = true; return;} If (g_isfind = false) path. pop_front () ;}} Binarynode * findpublicnode (binarynode * root, binarynode * LHS, binarynode * RHs) {If (! Root) return NULL; // find the LHS path and RHS path, respectively, and store them in path1link and path2linklist <binarynode *> path1; List <binarynode *> path2; stack <binarynode *> nodestack; int pathflag = 0; // first traverse the node and check whether the node postorderfindpath (root, LHS, path1) to be searched; g_isfind = false; postorderfindpath (root, RHS, path2); int d = path1.size ()-path2.size (); List <binarynode * >:: iterator longiter; List <binarynode * >:: iterator shortiter; if (D> 0) {longiter = path1. B Egin (); shortiter = path2.begin ();} else {// path2 step d First longiter = path2.begin (); shortiter = path1.begin ();} // longiter step d first, then compare equal for (INT I = 0; I <D; ++ I) longiter ++; while (* longiter! = * Shortiter) {longiter ++; shortiter ++;} return * longiter;} void preorder (binarynode * root) {If (! Root) return; stack <binarynode *> nodestack; binarynode * ptemp = root; while (ptemp |! Nodestack. empty () {If (ptemp) {cout <ptemp-> m_data <"\ t"; nodestack. push (ptemp); ptemp = ptemp-> m_left;} else {ptemp = nodestack. top (); nodestack. pop (); ptemp = ptemp-> m_right ;}} void testofpublicnode () {const int size = 10; int iarr [size]; initarr (iarr, size ); showarr (iarr, size); binarynode * root = buildbinarytree (iarr, 0, size-1); preorder (Root); cout <findpublicnode (root, (root-> m_left) -> m_left, (root-> m_left)-> m_right)-> m_data <Endl ;}

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.