Highbridge Capital Interview Question for Java Developers


Country: United States
Interview Type: In-Person




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

private static void preOrderTraversal(BinaryTreeNode root) {
		Stack<BinaryTreeNode> s = new Stack<BinaryTreeNode>();
		while (true) {
			while (root != null) {
				System.out.println(root.getData());
				s.push(root);
				root = root.getLeft();
			}
			if (s.isEmpty())
				return;

			root = (BinaryTreeNode) s.pop();
			root = root.getRight();
		}
	}

- Vir Pratap Uttam May 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Algo :
1) Push root into Stack
2) Now iterate steps 3,4,5 with condition - while(stack is not empty)
3) Pop a node, print it
4) if node->right is not NULL, push it
5) if node->left is not NULL, push it

Full working code here :
awesomecoder.blogspot.com/2012/08/preorder-traversal-of-bst-iteratively.html

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

The code goes like this:

void preorder(tree *T)
{ Stack S;
while(T!=NULL)
{push(&S, T);
T=T->left;
}
while(!empty(&S)
{
T=pop(&S);
printf("%d",T->data);
T=T->right;
while(T!=NULL)
{
push(&S, T);
T=T->left;
}
}
}

- Ashish August 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

You can understand the concept by watching this video

Iterative Preorder tree traversal
youtube.com/watch?v=uPTCbdHSFg4

Iterative postorder tree traversal
youtube.com/watch?v=hv-mJUs5mvU

Iterative inorder tree traversal
youtube.com/watch?v=50v1sJkjxoc

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

Why are you promoting your own videos ?

- Tim August 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It is funny they called this a pre-order traversal, for this type of traversal has a name which is called a level order traversal. It can also be implemented using a queue, which is actually a preferable implementation when doing a level order traversal on a tree.

- Anonymous August 22, 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