MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / lowestCommonAncestor

Method lowestCommonAncestor

LowestCommonAncestor.java:2–14  ·  view source on GitHub ↗
(TreeNode root, TreeNode p, TreeNode q)

Source from the content-addressed store, hash-verified

1class Solution {
2 public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
3
4 if(root == null || root == p || root == q)
5 return root;
6
7 TreeNode left = lowestCommonAncestor(root.left, p, q);
8 TreeNode right = lowestCommonAncestor(root.right, p, q);
9
10 if(left==null) return right;
11 else if(right==null) return left;
12 else return root;
13
14 }
15}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected