gul2u
BAN USER- 0of 0 votes
Answerspublic interface FirstCommonAncestor {
- gul2u in United States
/**
* Given two nodes of a tree,
* method should return the deepest common ancestor of those nodes.
*
* A
* / \
* B C
* / \ \
* D E M
* / \
* G F
*
* commonAncestor(D, F) = B
* commonAncestor(C, G) = A
*/
public Node commonAncestor(Node nodeOne, Node nodeTwo)
{
}
}
class Node {
final Node parent;
final Node left;
final Node right;
public Node(Node parent, Node left, Node right, data) {
this.parent = parent;
this.left = left;
this.right = right;
this.data = data
}
boolean isRoot() {
return parent == null;
}
}| Report Duplicate | Flag | PURGE
Linkedin Software Engineer / Developer Trees and Graphs