Code For NonGeek
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Find Lowest common ancestor of bst

Go down

Find Lowest common ancestor of bst Empty Find Lowest common ancestor of bst

Post  Admin 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

https://codefornongeek.forumotion.com

Back to top Go down

Back to top


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