Leaf at same Level
Traverse tree and set value of a leaf node and check with all leaf nodes whether the level == saved value.
Code:
int l=-1;
boolean p(Node root,int lvl)
{
if(root==null)
return true;
if(root.left==null && root.right==null)
{
if(l==-1)
{l=lvl;
return true;
}
else if(l==lvl)
return true;
return false;
}
return p(root.left,lvl+1) && p(root.right,lvl+1);
}
Code:
int l=-1;
boolean p(Node root,int lvl)
{
if(root==null)
return true;
if(root.left==null && root.right==null)
{
if(l==-1)
{l=lvl;
return true;
}
else if(l==lvl)
return true;
return false;
}
return p(root.left,lvl+1) && p(root.right,lvl+1);
}
0 Comments:
Post a Comment