Saturday, March 4, 2023
n
is a number of nodesh
is a height of a binary treefunc lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
if root == p || root == q || root == nil {
return root
}
left := lowestCommonAncestor(root.Left, p, q)
right := lowestCommonAncestor(root.Right, p, q)
if left == nil && right == nil {
return nil
}
if left != nil && right != nil {
return root
}
if (left != nil) {
return left
}
return right
}