Google Interview Question

Given a graph node, write a code to return a copy of the sub-graph starting with that node.

Interview Answers

Anonymous

Sep 4, 2012

binary tree is not the only type of graph. How would you handle cycles? What about self references etc?

1

Anonymous

Sep 8, 2012

This question is ill-defined. Is it an undirected graph a directed graph or a random graph whose edges are selected with probability p?

1

Anonymous

Oct 21, 2012

question is what is "sub-graph" start with a node

Anonymous

Jul 3, 2012

jk. But the code probably looks like this. GetSubTreeCopy(Node n) { Node n1= new Node(); n1.x=n.x; if(n.left !=null) { Node n2= GetSubTreeCOpy(n.left); n1.left=n2; } if(n.right !=null) { Node n2= GetSubTreeCOpy(n.right); n1.right=n2; } return n1; }