-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowestCommonAncestor.cs
More file actions
77 lines (64 loc) · 2.5 KB
/
LowestCommonAncestor.cs
File metadata and controls
77 lines (64 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using DataStructures.Libraries.Trees;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithms.Problem.Trees
{
[TestClass]
public class LowestCommonAncestor
{
public BinaryTreeNode Root { get; }
public LowestCommonAncestor()
{
}
public LowestCommonAncestor(BinaryTreeNode root)
{
this.Root = root;
}
public BinaryTreeNode FindLowestCommonAncestor(BinaryTreeNode node1, BinaryTreeNode node2)
{
BinaryTreeNode lowestCommonAncestorNode = null;
if(node1 == null && node2 == null)
{
return lowestCommonAncestorNode;
}
BinaryTreeNode node = this.Root;
while (node != null)
{
if (node.Value > node1.Value && node.Value > node2.Value)
{
node = node.Left;
}
else if(node.Value < node1.Value && node.Value < node2.Value)
{
node = node.Right;
}
else
{
lowestCommonAncestorNode = node;
break;
}
}
return lowestCommonAncestorNode;
}
[TestMethod]
public void ValidateTreeSpiralOrder()
{
var binarySearchTree = new BinarySearchTree();
binarySearchTree.PopulateDefaultBalanceTree();
LowestCommonAncestor lowestCommonAncestor = new LowestCommonAncestor(binarySearchTree.Root);
// Lowest common ancestor for 10 & 30 is 20
BinaryTreeNode lcaNode1 = lowestCommonAncestor.FindLowestCommonAncestor(new BinaryTreeNode(10),new BinaryTreeNode(30));
Assert.AreEqual(20, lcaNode1.Value, "The LCA value is incorrect!");
// Lowest common ancestor for 5 & 12 is 10
BinaryTreeNode lcaNode2 = lowestCommonAncestor.FindLowestCommonAncestor(new BinaryTreeNode(5), new BinaryTreeNode(12));
Assert.AreEqual(10, lcaNode2.Value, "The LCA value is incorrect!");
// Lowest common anscestor for 12 & 35 is 20
BinaryTreeNode lcaNode3 = lowestCommonAncestor.FindLowestCommonAncestor(new BinaryTreeNode(12), new BinaryTreeNode(35));
Assert.AreEqual(20, lcaNode3.Value, "The LCA value is incorrect!");
}
}
}