Lowest Common Ancestor
Last updated
Last updated
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// Time Complexity: O(n), n -> number of nodes
// Space Complexity: O(h), h -> Stack space
// root == null -> Reached leaf nodes
// root == p -> Found P node
// root == q -> Found Q node
// When we found what we want, we return the node itself as we do not
// want to explore further
if(root == null) return null;
if(root == p || root == q) return root;
// Explore left and right recursively
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
// If left node is given null, then pick right as we are not intersted in null values
// Vice verca for right node
// And for all other cases just return the current node
if(left == null) return right;
else if(right == null) return left;
else return root;
}
}