public static void Main()
Node Core = new Node() { Value = "Core", ParentNode=null};
Node First = new Node(){ Value = "First", ParentNode=Core};
Node A1 = new Node(){ Value = "A1", ParentNode= First};
Node A2 = new Node(){ Value = "A2", ParentNode= First};
Node Second = new Node(){ Value = "Second", ParentNode=Core};
Node B1 = new Node(){ Value = "B1", ParentNode= Second};
Node B2 = new Node(){ Value = "B2", ParentNode= Second};
Node Third = new Node(){ Value = "Third", ParentNode=Core};
Node C1 = new Node(){ Value = "C1", ParentNode= Third};
Node CC3 = new Node(){ Value = "CC3", ParentNode= C1};
Node CC4 = new Node(){ Value = "CC4", ParentNode= C1};
Node CC5 = new Node(){ Value = "CC5", ParentNode= C1};
Node C2 = new Node(){ Value = "C2", ParentNode= Third};
Node C3 = new Node(){ Value = "C3", ParentNode= Third};
Node CC1 = new Node(){ Value = "CC1", ParentNode= C3};
Node CC2 = new Node(){ Value = "CC2", ParentNode= C3};
Node C4 = new Node(){ Value = "C4", ParentNode= Third};
Node Forth = new Node(){ Value = "Forth", ParentNode=Core};
Node D1 = new Node(){ Value = "D1", ParentNode= Forth};
Node D2 = new Node(){ Value = "D2", ParentNode= Forth};
Node D3 = new Node(){ Value = "D3", ParentNode= Forth};
Node Fifth = new Node(){ Value = "Fifth", ParentNode=Core};
Node E1 = new Node(){ Value = "E1", ParentNode= Fifth};
Node E2 = new Node(){ Value = "E2", ParentNode= Fifth};
Node EE1 = new Node(){ Value = "EE1", ParentNode= E1};
Node EEE1 = new Node(){ Value = "EEE1", ParentNode= EE1};
Node EE2 = new Node(){ Value = "EE2", ParentNode= E1};
Node EE3 = new Node(){ Value = "EE3", ParentNode= E1};
Console.WriteLine("Tree Generated");
Node Result = FindCommonParent(A1, A2);
Console.WriteLine("Common Parent : " + Result.Value);
public static Node FindCommonParent(Node input1, Node input2)
int level1 = GetLevel(input1);
int level2 = GetLevel(input1);
Node Result = new Node();
Console.WriteLine("Level1 : " + level1.ToString());
Console.WriteLine("Level2 : " + level2.ToString());
for(int i=level1; i>=level2; i--)
Console.WriteLine("Parent1 : " + input1.Value);
Console.WriteLine("Parent2 : " + input1.Value);
input1 = GetParentNode(input1);
if(input1.Value == input2.Value)
Result.Value = input1.Value;
for(int i=level2; i>=level1; i--)
input2 = GetParentNode(input2);
if(input1.Value == input2.Value)
Result.Value = input1.Value;
public static int GetLevel(Node input)
public static Node GetParentNode(Node input)
Node Parent = new Node();
if(input.ParentNode != null)
Parent.Value = input.ParentNode.Value;
Parent.ParentNode = input.ParentNode.ParentNode;
public string Value {get; set;}
public Node ParentNode { get; set;}