Amazon Interview Question


Country: United States




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

As alex said a BST can be reconstructed from the preorder traversal alone.

It appears the question should actually have been for a binary tree. For binary trees none of three traversals inorder, preorder or postorder alone can uniquely identify the tree. For a binary tree inorder traversal and either a postorder or preorder traversal can uniquely identify the tree.

Importance/Requirement of Inorder traversal:
The inorder sequence will resolve the left-right problem, and the other sequence (pre or post) will tell us the roots of the various subtrees. For example, if inorder is CBA, and preorder is CAB, then we know that C is at the root, and both B and A are in the right subtree

- lakshaman January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Suppose if u persist preorder only postorder than how would u identify that which element will come to the left or which element will come to the right.
2. this will lead us to the different BST from the one which we persisted.
3.So by using only one traversal we will get differnet BSTs with same elements.

- saumya.tyagi6 January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this is turning around the question and repeating it...not a reasoning

- anon123 January 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Nothing makes it mandatory. BST is restorable from preorder in O(n).

- alex January 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is true, if you include the NULL nodes.

- Anonymous January 24, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

dcsdcsdcscsdcsdcsdcdsc

- Anonymous January 22, 2014 | 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