IBM Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

Do you mean to find the mirror ? If that, then you can exchange the left with the right ptr and recurse down both the subtrees.

- knap August 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yup, i do feel it had to be mirror..otherwise it doesn't makes sense to me...correct me if i m wrong..

- yogesh August 15, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Following is the recursive solution to the problem...

node *mirror(node *root)
{
  node *temp;
  
  if(root == NULL) 
	return NULL;
   
  temp = (node *)malloc(sizeof(node));

  temp->data  = root->data;
  temp->left  = mirror(root->right);
  temp->right = mirror(root->left);

  return temp;
}

- Samwise August 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a
b c
d e f g is the input

the output should be
d e f g
b c
a

this is what it means by reversing a binary tree

- Anonymous August 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it should not mean that u r simply printing it in levelorder ok
u need to adjust the links
so the code is required to adjust the links recursively!!

- Anonymous August 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

in that case chk this out...
http://www.careercup.com/question?id=60068

- Samwise August 17, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it has be a mirror only,,,else the Qs is wrong.
btw, the simplest/easiest solution is just to keep rotating the left & right pointers at each step of forming the new tree...thats it...look at the link in Samwise's reply...

- googler August 18, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my code in C#.
A list is used to store the leaf node (which finally becomes roots).
Based on Post-Order traversal and it works.
May be you can add code to make initial root.left = root.right = null.
I tested and it works :)

public List<node> list = null;
public void Reverse()
{
list = new List<node>();
ReverseTree(root, null);
}

private void ReverseTree(node rootptr,node rootroot)
{
node temp;
if (rootptr.left != null)
{
ReverseTree(rootptr.left,rootptr);
}

if (rootptr.right != null)
{
ReverseTree(rootptr.right,rootptr);
}

if ((rootptr.left == null) && (rootptr.right == null))
{
list.Add(rootptr);
}

if (rootroot != null)
rootptr.left = rootroot;
}

- pk August 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

struct node* mirror(struct node*T , struct node*T2)
{
if(T==NULL)return NULL;

T2->data=T->data;

if(T->left!=NULL)
T2->right=mirror(T->left);

if(T->right!=NULL)
T2->left=mirror(T->right);

return T2;

}

- Anonymous September 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def reverse(t):
    if "L" not in t and "R" not in t:
        return t
    t["L"], t["R"] = reverse(t["R"]), reverse(t["L"])
    return t

- Gnu January 07, 2010 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More