8-10-12-02-22.29.c

Source: Internet
Author: User

A simulated function aims to find the nearest common ancestor of a pair of nodes. the implementation idea is to map the tree to an array first, and set the value range with the node to root, and the value ranges of other nodes to the parent node index, so that you can easily find the parent node. therefore, an obvious simulated function appears.

/* 8-10-12-02-22.29.c -- Chapter 8: Question 10 */</P> <p>/* this is a model that can return a proximate communal parent betwen two node */<br/>/* first, map a tree to an array */</P> <p> elementtype findcommunalparent (const elementtype * const array, const int V, const int W ); </P> <p> elementtype findcommunalparent (const elementtype * const array, const int V, con St int W) <br/>{< br/> If (root = v | root = W) <br/> return failed; <br/> If (array [v] <array [w]) <br/> {<br/> while (W! = Root & array [v]! = Array [w]) <br/> W = array [w]; <br/>}< br/> else if (array [v]> array [w]) <br/>{< br/> while (V! = Root & array [v]! = Arrat [w]) <br/> V = array [v]; <br/>}< br/> else <br/> return array [v]; /* or array [w] */<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.