Recent common ancestor

Source: Internet
Author: User

References 1

References 2

Question: enter two nodes in the binary tree and output the lowest common parent node among the two nodes.

Variant 1: The binary tree is a binary search tree. If the root node is smaller than the two nodes, the right subtree is accessed. If both nodes are larger, the left subtree is accessed. Otherwise, the result is displayed.

Variant 2: If each node has a pointer to the parent node, it is converted to the first public node for searching two single-chain tables.

Variant 3: for a common binary tree, the following two methods are provided:

Method 1: perform post-order traversal of a binary tree. Check whether the node is in the left subtree and then the right subtree. Then, determine the position of the ancestor node based on the number of nodes contained in the left and right subtree.

Struct binarytree {int value; binarytree * left; binarytree * right; binarytree (int x): Value (x), left (null), right (null ){}}; binarytree * LCA (binarytree * root, binarytree * node1, binarytree * node2) {If (root = NULL) return NULL; If (root = node1 | root = node2) return root; binarytree * Left = LCA (root-> left, node1, node2); binarytree * Right = LCA (root-> right, node1, node2 ); if (left & right) return root; // If the left and right sides are not empty, it indicates that each left and right subtree has a node if (Left = NULL) return right; // both nodes are in the right subtree return left; // both nodes are in the left subtree}

Method 2: Use recursive traversal to obtain the path from the root node to the two nodes, and then determine the common ancestor. The idea is to simulate variant 2.

Bool getroot2nodepath (binarytree * root, binarytree * node, vector <binarytree *> & Path) // obtain the path from root to node {If (root = node) return true; Path. push_back (Root); bool flag = false; If (root-> left) Flag = getroot2nodepath (root-> left, node, PATH); If (! Flag & root-> right) Flag = getroot2nodepath (root-> right, node, PATH); If (! Flag) path. pop_back (); Return flag;} binarytree * LCA (binarytree * root, binarytree * node1, binarytree * node2) {If (root = NULL) return NULL; vector <binarytree *> path1, path2; getroot2nodepath (root, node1, path1); getroot2nodepath (root, node2, path2); binarytree * P = NULL; int I = 0; while (I <path1.size () & I <path2.size () {If (path1 [I] = path2 [I]) P = path1 [I ++]; else break;} return P ;}




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.