using System;
class Node
{
public Node left = null;
public Node right = null;
public int data;
public Node(int data)
{
this.data = data;
}
}
public class GFG{
static int findDepth(Node root)
{
if (root == null)
return 0;
int left = findDepth(root.left);
int right = findDepth(root.right);
return 1 + Math.Max(left, right);
}
static Node DFS(Node root, int curr,
int depth)
{
if (root == null)
return null;
if (curr == depth)
return root;
Node left = DFS(root.left, curr + 1, depth);
Node right = DFS(root.right, curr + 1, depth);
if (left != null && right != null)
return root;
return (left != null) ? left : right;
}
static Node lcaOfDeepestLeaves(Node root)
{
if (root == null)
return null;
int depth = findDepth(root) - 1;
return DFS(root, 0, depth);
}
public static void Main(String[] args)
{
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
root.right.right = new Node(7);
root.right.left.left = new Node(8);
root.right.left.right = new Node(9);
Console.WriteLine(lcaOfDeepestLeaves(root).data);
}
}