LCA of two tree nodes
5
9 11
2 3 7
1 Last updated
5
9 11
2 3 7
1 Last updated
public TreeNode findLCA(TreeNode one, TreeNode two){
//sanity check
if ( one == null || two == null )return null;
TreeNode root = one;
while ( root.parent != null ){
root = root.parent;
}
TreeNode result = LCAhelper(root, one, two)
return result;
} public TreeNode LCAhelper(TreeNode root, TreeNode one, TreeNode two){
if ( root == null ) return null;
if ( root == one || root == two) return root;
TreeNode leftCheck = LCAhelper(root.left, one, two);
TreeNode rightCheck = LCAhelper(root.right, one, two);
//return non null node up tree
if (leftCheck == null) return rightCheck;
if (rightCheck == null) return leftCheck;
return root;
}