Shashank
BAN USERAlgorithm :
-find depth of each node recursively
-set a variable with depth of first leaf (when traversing)
- if a node is a leaf ,check its depth value with the previously set value
- if all the leaves obey , all leaves are at same level ,
- if any case doesn't obey return -1 breaking recursion. so when -1 is returned by fn then its imperfect , if 0 then all leaves are at same level.
here is the code:
int depth(node *t, int d, int depth)
{
if(!(t->left) && !(t->right)) // this is the leaf
{
if(first==true)// first leaf node
{
first=false;
depth=d;
}
else
{
if(d!=depth)
return -1;
}
return 0;
}
else
{
if(depth(t->left,d+1,depth)==0 && depth(t->right,d+1,depth)==0)
return 0;
else
return -1;
}
}
Repabbyhgabb, Associate at Abs india pvt. ltd.
Develop fresh content for emails, web banners, site wide communication, and subject lines.Write stories, blogs and related content about ...
RepSpent 2001-2004 selling UFOs for the government. Have some experience with Internet Marketing Services New York. Set new standards for ...
Very Easy question ,
Algorithm:
- given a node in a bst, the rightmost child of the given node should point to the parent of the current node.
- the left child of the current node is to point to the parent(using the right link), and make left like of the curreent node equals to null.
- do this recursively for all nodes
NOTE : the trick is to parse the tree from its leftmost end (DFS)
- Shashank October 06, 2013