Here is the code:
class Program
{
static void Main(string[] args)
{
BSTnode n = new BSTnode(5);
n.add(2);
n.add(7);
n.add(3);
n.add(1);
n.add(6);
n.add(8);
n.add(9);
BSTnode n2 = new BSTnode(5);
n2.add(2);
n2.add(7);
n2.add(3);
n2.add(1);
n2.add(7);
n2.add(8);
n2.add(9);
BSTnode n3 = new BSTnode(5);
n3.add(2);
n3.add(7);
n3.add(3);
n3.add(1);
n3.add(6);
n3.add(8);
n3.add(9);
Console.WriteLine(areTheyIdentical(n, n2));
Console.WriteLine(areTheyIdentical(n, n3));
}
public static bool areTheyIdentical(BSTnode b1, BSTnode b2)
{
if (b1.data != b2.data)
{
return false;
}
else
{
if ((b1.LeftNode != null && b2.LeftNode == null) || (b1.LeftNode == null && b2.LeftNode != null))
{
return false;
}
if (b1.LeftNode != null && b2.LeftNode != null)
{
if (!areTheyIdentical(b1.LeftNode, b2.LeftNode))
{
return false;
}
}
if ((b1.rightNode != null && b2.rightNode == null) || (b1.rightNode == null && b2.rightNode != null))
{
return false;
}
if (b1.rightNode != null && b2.rightNode != null)
{
if (!areTheyIdentical(b1.rightNode, b2.rightNode))
{
return false;
}
}
}
return true;
}
}
public class BSTnode
{
public int data { get; set; }
public BSTnode LeftNode { get; set; }
public BSTnode rightNode { get; set; }
public BSTnode(int _data)
{
data = _data;
}
public void add(int _data)
{
BSTnode newN = new BSTnode(_data);
if (_data < data)
{
if (LeftNode != null)
{
LeftNode.add(_data);
}
else
{
LeftNode = newN;
}
}
else
{
if (rightNode != null)
{
rightNode.add(_data);
}
else
{
rightNode = newN;
}
}
}
}
This Solution is written by me and is not completely tested. Please comment if you found any alternative solutions or any enhancement to my solution.
Popular Posts
-
Here is the code: // => Brute Force Solution: O(m* n) static long arrayManipulation(int n, int[][] queries) { ...
-
Whatever it is one fine day everyone on this planet who are born have to die for sure. When you are close ones are with you, you wont know ...
-
HTML: <div class="outer-container"> <div class="inner-container"> <div class="t...
No comments:
Post a Comment