DDSA
Advertisement

236. Lowest Common Ancestor of a Binary Tree

Time: O(n)
Space: O(h)

Approach

Recursive — if root equals p or q return it; if p and q are found in different subtrees the current root is the LCA.

236.cs
C#
// Approach: Recursive — if root equals p or q return it; if p and q are
// found in different subtrees the current root is the LCA.
// Time: O(n) Space: O(h)

public class TreeNode
{
    public int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int x) { val = x; }
}

public class Solution
{
    public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
    {
        if (root == null || root == p || root == q)
            return root;

        TreeNode left = LowestCommonAncestor(root.left, p, q);
        TreeNode right = LowestCommonAncestor(root.right, p, q);

        if (left == null)
            return right;
        if (right == null)
            return left;
        else
            return root;
    }
}
Advertisement
Was this solution helpful?