Lowest Common Ancestor of a Binary Tree II

Asked atAmazon
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes p and q. If either p or q does not exist in the tree, return null.

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 Output: 3 Explanation: The LCA of nodes 5 and 1 is 3.

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 Output: 5 Explanation: The LCA of nodes 5 and 4 is 5.

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 9

  • The number of nodes in the tree is in the range [1, 104]
  • -10⁹ ≤ Node.val ≤ 10⁹
  • All Node.val are unique.
  • p ≠ q

cpp

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     int data;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : data(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        //Your Code Goes Here
    }
};

java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int data;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        // Your Code Goes Here
    }
}

python

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.data = x
#         self.left = None
#         self.right = None

class Solution:
    def lowestCommonAncestor(self, root, p, q):

javascript

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *      constructor(val = 0, left = null, right = null){
 *          this.data = val;
 *          this.left = null;
 *          this.right = null;
 *      }
 * }
 **/

class Solution {
    lowestCommonAncestor(root,p,q) {
        //your code goes here
    }
}

csharp

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val) { this.val = val; this.left = this.right = null; }
 * }
 */
public class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
       //your code goes here
    }
}

go

func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
    // Your code goes here
}
Stuck? Show a way to structure it+
  1. 01Define a DFS that returns an LCA candidate from a subtree.
  2. 02Return the node itself when it matches p or q.
  3. 03Combine non-null results from left and right children.
  4. 04Track whether each target was actually encountered.
  5. 05Return the candidate only when both targets exist.

Reference answer

Then expect these follow-ups

  • How would you avoid recursion overflow on a skewed tree?

    Tests: iterative traversal

  • How does the answer change in a BST?

    Tests: tree properties

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question