Interview Question


Country: United States




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

PrintBorder(Node root)
{
    PrintNode(root);
    PrintBorderInternal(root.Left, true /* is border */, 0 /* left branch */);
    PrintBorderInternal(root.Right, true /* is border */, 1 /* right branch */);
}

// direction = 0 for left, 1 for right
PrintBorderInternal(Node node, bool isBorder, int direction)
{
    if (node == null)
        return;

    // If the node is a LEAF node, or is a border, print it.
    if (isBorder || (node.Left == NULL && node.Right == NULL))
    {
        PrintNode(node);
    }

    PrintBorderInternal(Node.Left, isBorder && direction == 0, direction);
    PrintBorderInternal(Node.Right, isBorder && direction == 1, direction);
}

- Anonymous May 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

To print in the right order, wouldn't you need to do the "if (isBorder) print" before the recursive calls if direction is left, and after the calls if the direction is right?

- Martin May 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

At the root node, use preorder for the left side, and use postorder in the right side, then the output will be in the correct order.

- sqw May 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Preorder on the root->left and post order on the root->right and then accessing root would give the correct order.

- nithin May 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do an in order traversal of the tree and build a LinkedHashMap of nodes at each level. For level 0, there will only be one node. For the last level all nodes are in the border. For all internal levels first and last nodes are on the border.

Complexity of time and space is O(n). You can optimize the space complexity further but another time

- border May 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do a BFS... At every point print first and last Node. Except last where you will priont the whole Queue. Although order will be little different then you mentioned but even that is easy to format by maintaining the list of border nodes instead of printing right away.
Time complexity o(n) space complexity is O(log n)

- loveCoding May 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Keep printing all the elements in extreme left and all the elements in extreme right and leaf node.
Leave those elements which come from moving left to right or right to left.

Here is a code for the same

void display(struct node* root, int left, int right)
{
if( (left) || (right) || !((root->right)||(root->left)) )
printf("%d\n",root->data);
return;
}

void findborder(struct node* root, int left, int right)
{

if(root != NULL)
{
display(root,left,right);
findborder(root->left,left&1,0);
findborder(root->right,0,right&1);

}

else
{
return 0;
}

}

- Anonymous May 16, 2012 | 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