Walmart Labs Interview Question
Software Engineer / DevelopersTeam: Services
Country: India
Interview Type: In-Person
Even if root->right is NULL.. it ll work.. NULL value ll be assigned to root->left tht is same as tht of not assigning it(No Error).
@kushalbafna, you still have to assign parent's right pointer to null in the else part..that's what Tanmay had mentioned..
private static Tree<Integer> convertLeftChildRightSiblingTree(
Tree<Integer> tree) {
// TODO Auto-generated method stub
if(tree==null)
return null;
convertLeftChildRightSiblingTree(tree.left);
convertLeftChildRightSiblingTree(tree.right);
if(tree.left!=null){
tree.left.right=tree.right;
}
else{
tree.left=tree.right;
}
tree.right=null;
return tree;
}
Nope......you should transform in such a way that left child of a node should point to its sibling (i.e. right child) and parent node's right should be null. You should not reconstruct the tree but make use of the current node structure.
BST * siblingsTransform(BST *root)
- kushalbafna November 14, 2012{
if(root == NULL)
return NULL;
siblingsTransform(root->left);
siblingsTransform(root->right);
if(root->left !=NULL)
{
root->left ->right = root->right;
root->right = NULL;
}
else
{
root->left = root->right;
}
return root;
}