static Node LCA(Node root, Node p, Node q) {
if(root == p || root == q)
Node left = LCA(root.left, p, q);
Node right = LCA(root.right, p, q);
if(left != null && right != null)
return (left != null ? left : right);
public static void Main()
Node root = new Node('a');
root.left = new Node('b');
root.right = new Node('c');
root.left.left = new Node('d');
root.left.right = new Node('e');
root.left.left.left = new Node('h');
root.left.left.right = new Node('i');
root.right.left = new Node('f');
root.right.left.left = new Node('j');
root.right.left.right = new Node('k');
root.right.right = new Node('g');
root.right.right.right = new Node('l');
root.right.right.right.left = new Node('m');
root.right.right.right.right = new Node('n');
Console.Write(LCA(root, root.left, root.left.right).data);