Solving the lowest common ancestor node in binary search tree

Source: Internet
Author: User

One, the problem description

Construct a binary lookup tree, and given two nodes, find the lowest common ancestor node for both nodes.

This assumes that the weight of the nodes in the binary lookup tree is the integer number (see the Binarynode inner class in the code), the lowest common ancestor node is as follows: node 5 and Node 12 's lowest common ancestor node is node 10.

Second, realize the idea

Assuming that the weights of the given two nodes are Node1 and Node2, respectively

If the weight of the root is between Node1 and Node2, then the root is their lowest common ancestor node.

If the weights of the roots are larger than Node1 and Node2, then their lowest common ancestor nodes are at the root of the record

If the weight of the root is smaller than Node1 and Node2, their lowest common ancestor node is in the right subtree of the root

Therefore, this can be achieved by recursive return.

Third, the code implementation

First we have to construct a binary search tree. The concrete structure can be referenced: the construction of two-fork tree

Once constructed, call the Lowestcommonparentnode method to find the lowest common ancestor node weights.

 Public classLowcommonnode {Private classbinarynode{Binarynode left;        Binarynode right; intEle;  PublicBinarynode (intele) {             This. Ele =Ele; Left= right =NULL; }    }        PrivateBinarynode Root; Private voidBuildtree (int[] arr) {         for(intI:arr)        {Insert (i); }    }    Private voidInsertintele) {Root=Insert (root, ele); }    PrivateBinarynode Insert (Binarynode root,intele) {        if(Root = =NULL)            return NewBinarynode (ele); if(Root.ele > Ele)//Insert LeftRoot.left =Insert (Root.left, ele); Else if(Root.ele <ele) Root.right=Insert (root.right, ele); ElseRoot.left= Insert (Root.left, ele);//when equal, put it on the left        returnRoot; }        /*** Solve the lowest common ancestor node of the nodes represented by Node1 and Node2 in the binary lookup tree * First let the node1 always represent that node with the smaller weights. * For binary search trees: * If the root weights are between Node1 and Node2, then the root is their lowest common ancestor node * If the root weights are larger than Node1 and Node2, then their lowest common ancestor nodes are in the left subtree of the root * if the root Weights are smaller than node1 and Node2, their lowest common ancestor nodes are in the right subtree of the root*/     Public intLowestcommonparentnode (Binarynode root,intNode1,intNode2) {        if(Node1 >Node2) {            intTMP =Node1; Node1=Node2; Node2=tmp; }        assertNode1 <Node2; if(Root = =NULL)            Throw NewIllegalArgumentException ("Neither Node1 nor Node2 contains in binary search tree"); if(Root.ele > Node1 && root.ele <Node2)returnRoot.ele; if(Root.ele > Node1 && root.ele > Node2)//if (Root.ele > Node2)//find the lowest common ancestor node in the left subtree            returnLowestcommonparentnode (Root.left, Node1, Node2); Else//Root.ele < Node1//find the lowest common ancestor node in the right subtree            returnLowestcommonparentnode (Root.right, Node1, Node2); }        //Hapjin Test     Public Static voidMain (string[] args) {Lowcommonnode LCN=NewLowcommonnode (); int[] arr = {20,10,30,5,15,25,40,12,18}; Lcn.buildtree (arr);//Build a binary search tree//Node1 and Node2 should exist in Arr,or would throw IllegalArgumentException        intNode1 = 5; intNode2 = 12; //should build Tree before invoke LowestcommonparentnodeSystem.out.println (Lcn.lowestcommonparentnode (Lcn.root, Node1, Node2)); }}

Four, references

Solving the lowest common parent node of two nodes in binary tree

The construction of binary tree

Solving the lowest common ancestor node in binary search tree

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.