Bloomberg LP Interview Question
Financial Software DevelopersCountry: United States
Interview Type: In-Person
public class ReverseBinTree {
/**
* @param args
* @author sandeep
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryTree seven = new BinaryTree(7, null, null);
BinaryTree six = new BinaryTree(6, null, null);
BinaryTree three = new BinaryTree(3, six, seven);
BinaryTree five = new BinaryTree(5, null, null);
BinaryTree four = new BinaryTree(4, null, null);
BinaryTree two = new BinaryTree(2, four, five);
BinaryTree root = new BinaryTree(1, two, three);
revBinTree(root);
System.out.println("The reversed tree is " + root.toString());
}
private static void revBinTree(BinaryTree root) {
// TODO Auto-generated method stub
if(root != null){
BinaryTree temp = root.left;
root.left = root.right;
root.right = temp;
revBinTree(root.left);
revBinTree(root.right);
}
}
}
void MirrorImage(Node * root)
{
if(root){
Node * temp = root->left
root->left = root->right;
root->right = temp;
MirrorImage(root->left);
MirrorImage(root->right);
}
}
to create a mirror image we need to traverse the tree in ordrer: root->right->left
function mirror(root,mirror){
if(root==NULL)return;
mirror.val = root.val;
mirror( root.right,mirror.left)
mirror( root.left, mirror.right)
}
OR you can swap the right node with left node to change the same tree
function mirror(root){
if(root==NULL)return;
swap(root.left,root.right);
mirror(root.left)
mirror(root.right)
}
It is not a correct question.
Binary tree is not a geometrical object to create mirror images of it.
Binary tree is a node-based binary tree data structure which has the following properties:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and right subtrees must also be binary search trees.
If you call "mirror" function that people give here on an existing binary tree, the resulting object will not be a binary tree at all, it will have wrong ordering of the nodes.
I think they wanted to ask such a question:
"Print binary tree level by level so elements of each level will be in the reverse order".
public static void mirror(Node root) {
if (root.left() == null && root.right() == null) return;
exchange(root, root.left(), root.right());
mirror(root.left());
mirror(root.right());
}
private static void exchange(Node root, Node left, Node right) {
Node temp = root.left();
root.left(root.right());
root.right(temp);
}
I think for this question it requires to "create" a mirror image , so may we should create them a new tree
node *mirror(struct node *root){
if(root!=NULL){
node *result=new node;
(*result)->leftch=NULL;
(*result)->rightch=NULL; //initialization
(*result).data=root->data;
(*result)->rightch=mirror(root->rightch);
(*result)->leftch=mirror(root->leftch);
return result;
}
else{
return NULL;
}
}
struct node* mirror(struct node*root)
- Ali_BABA January 21, 2012{
struct node*temp;
if(root!=NULL)
{
mirror(root->left);
mirror(root->right);
temp=root->left;
root->left=root->right;
root->right=temp;
}
return root
}