Lowest Common Ancestor
Page 1 of 1
Lowest Common Ancestor
Node LCA(Node root, Node p, Node q){
if(root==null)
return null;
if(root==p||root==q)
return root;
Node left=LCA(root.left, p, q);
Node right=LCA(root.right, p, q);
if(left!=null&&right!=null)
return root;
return left!=null?left:right;
}
if(root==null)
return null;
if(root==p||root==q)
return root;
Node left=LCA(root.left, p, q);
Node right=LCA(root.right, p, q);
if(left!=null&&right!=null)
return root;
return left!=null?left:right;
}
yangwenzhou- Posts : 5
Join date : 2011-09-23
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum