Amazon Interview Question for Software Engineer / Developers






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

Isn't the solution of this one the same as quesiton#325695?

- Anonymous February 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@above
No, This is different from question#325695.
In this question, all nodes in tree will be connected through next while in the previous question, only nodes at same levels will be connected through next.

- Nick February 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can do this during inorder traversal itself....keep a global pointer to prev node...

Node * prev;
inorder(Node* root) {
if(root->left != null)
inorder(root->left);

if(prev ==null )
prev= root;
else
{
prev->next = root;
prev= root;
}

if(root->right !=null)
inorder(root->right);
}

- A February 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Perfect!!

- clrs March 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice idea but following additions needed

if(prev==null)
{
head=root;
prev=root;
}
then only it will work.. now we can start from head to end.

- BVarghese July 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

en.wikipedia.org/wiki/Threaded_binary_tree

- luvtheedragon February 15, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I thought of this solution -

node_t *BSTNext(node_t *node){

}

- vj February 16, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is very similar to the last post -

node_t *BSTNext(node_t *node){
if(node == null)
return null;

node_t *left = BSTNext(node->left);
if (left != null)
left->next = node;

node_t *right = BSTNext(node->right);
if (node->right != null)
node->next = right;

if(right != null)
return right;
else
return node;
}

- vj February 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No. That is completely wrong.

- Ryan February 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void threadBST() {
  stack<Node*> s;
  Node* node = root;
  Node* prev = NULL;

  while (true) {
    if (node != NULL) {
      s.push(node);
      node = node->left;
    } else {
      if (!s.empty()) {
        node = s.top();
        s.pop();
        if (prev != NULL) prev->next = node;
        prev = node;
        node = node->right;
      } else {
        break;
      }
    }
  }
}

- Ryan February 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hi ryan can u pls explain ur code

- cu February 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

point in this solution is to move from right to left because i don't have next from current node. Use global to set current to next and move to left.

node* next = null;
void linkNext(node* n)
{
if(n == null) return;

linkNext(n->right)
n->next = next;
next = n;
linkNext(n->left)
}

- simpler March 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Morris In-order Traversal is used here
or
threaded binary tree

public static void MorrisTraversal(Node root)
        {
            Node current, pre;

            if (root == null)
                return;

            current = root;
            while (current != null)
            {
                if (current.Left == null)
                {
                    if (head1 == null)
                    {
                        head1 = current;
                        tail1 = current;
                    }
                    else
                    {
                        tail1.Next = current;
                        tail1 = current;
                    }
                    Console.WriteLine(current.ID);
                    current = current.Right;
                }
                else
                {
                    /* Find the inorder predecessor of current */
                    pre = current.Left;
                    while (pre.Right != null && pre.Right != current)
                        pre = pre.Right;

                    /* Make current as right child of its inorder predecessor */
                    if (pre.Right == null)
                    {
                        pre.Right = current;
                        current = current.Left;
                    }

                    /* Revert the changes made in if part to restore the original
                      tree i.e., fix the right child of predecssor */
                    else
                    {
                        pre.Right = null;

                        if (head1 == null)
                        {
                            head1 = current;
                            tail1 = current;
                        }
                        else
                        {
                            tail1.Next = current;
                            tail1 = current;
                        }
                        Console.WriteLine(current.ID);
                        current = current.Right;
                    } 
                } 
            } 
        }

        }

- BVarghese July 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Inorder using stack and setting next when you hit left child as nul
l

public void populateNextUsingInOrder(TreeNode root){
		if(root == null) return;
		TreeNode node = root;
		Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
		while(node != null){
			stack.push(node);
			node = node.getLeftChild();
		}
		node = stack.peek();
		TreeNode current = null;
		TreeNode temp = null;
		while(!stack.isEmpty()){
			current = stack.pop();
			System.out.print(current.getKey()+ "--> " );
			temp = current.getRightChild();
			while(temp != null){
				stack.push(temp);
				temp = temp.getLeftChild();
			}
			current.setNext(stack.peek());
		}
		/*** FOR TESTING ***/
		System.out.println("\nTraverse using next from min");
		while(node != null){
			System.out.print(node.getKey()+ "--> " );
			node = node.getnext();
		}
	}

- Balaji October 21, 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