Clean Java solution with explanation

  • 4
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
      if (root == null || root == p || root == q)
        // found p or q or touch the ground
        return root;
      // search p and q from left and right
      TreeNode left = lowestCommonAncestor(root.left, p, q);
      TreeNode right = lowestCommonAncestor(root.right, p, q);
      if (left != null && right != null)
        // from root's left & right we found both p and q, so root is the LCA
        return root;
        // left is not null means from left's left & right we found both q and q
        // so left is the LCA, otherwise, right is the answer
        return left != null ? left : right;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.