Created
March 14, 2013 22:31
-
-
Save KodeSeeker/5165843 to your computer and use it in GitHub Desktop.
Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree Avoid storing additional nodes in a data structure NOTE: This is not necessarily a binary search tree
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Approach similar to approach for Common ancestor for BST. Keep going down on one side(right or left) if both the nodes | |
are in the same side(right or left), else the current node is the lowest common ancestor*/ | |
public Tree commonAncestor(Node root, Node p, Node q) { | |
// 1st check if p and q, both belong to the left of the current root node, if yes then recurse on the left side | |
if (covers(root.left, p) && covers(root.left, q)) // check out the covers subroutine, can be used elsewhere too! | |
return commonAncestor(root.left, p, q); | |
// else, check if both p and q are children of right side of the root, if yes, then recurse on the right side | |
if (covers(root.right, p) && covers(root.right, q)) | |
return commonAncestor(root.right, p, q); | |
return root; // lowest Common Ancestor. | |
} | |
private boolean covers(Node root, Node p) { /* is p a child of root? */ | |
if (root == null) return false; | |
if (root == p) return true; | |
return covers(root.left, p) || covers(root.right, p); // recursively check if p is a child of right. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
plzzz give full code