Find Lowest common ancestor of bst

View previous topic View next topic Go down

Find Lowest common ancestor of bst

Post  Admin on Wed Aug 24, 2011 12:15 pm

public static TreeNode findancestor(TreeNode root, TreeNode n1, TreeNode n2){
if(cover(root.left,n1)&&cover(root.left,n2))
return findancestor(root.left,n1,n2);
if(cover(root.right,n1)&&cover(root.right,n2))
return findancestor(root.right,n1,n2);
return root;
}

public boolean cover(TreeNode root, TreeNode n1){
if(root==null)
return false;
else if(root==n1)
return true;
return cover(root.left,n1)||cover(root.right,n1);
}

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum