Amazon Interview Question


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
4
of 6 vote

Do a In order traversal push all the leaf nodes to the queue.

During pop assign the right pointer of the node to the next node.

TC : O(n) and SC : O(n)

- Shiva September 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

You dont have to store all the nodes in the in-order traversal. You could just remember the last encountered leaf node and assign the right pointer when you encounter the next leaf node.

This way, the SC would be O(1). However, TC remains the same.

- prasanth September 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@prasanth
Good suggestion

- Itanium September 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

do reverse inorder traversal

void SetLeafRighttonextLeaf(struct Node *node)
{
static struct Node * next = NULL;
if(!node)
return;
SetLeafRight(node->right);
if(node->left == NULL && node->right == NULL)
{
node->right = next;
next = node;
}
SetLeafRight(node->left,prev);
}
}

- Ashish September 19, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@prasanth : The extra argument still requires additional stack space on top of a recursive In order traversal that requires O(N) space.

- Raj September 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@raj, the stack space would take only the space equivalent to height of the tree. In the average case, it would be logN. All I suggested was that we need not store all the leaf nodes, but just the last leaf node.

- prasanth September 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<iostream>
using namespace std;

class TreeNode{
  public:
	TreeNode* left;
	TreeNode* right;
    int val;
	TreeNode(int);
};

TreeNode::TreeNode( int value ) {
	left = NULL;
	right = NULL;
	val = value;
}

void customInorderTraversal(TreeNode *node , TreeNode **prevleaf){

	if(node->left == NULL && node->right == NULL){
		if(*(prevleaf) != NULL){
			(*(prevleaf))->right = node;
		}
		*(prevleaf) = node;
	}else if(node->left == NULL){
		customInorderTraversal(node->right,prevleaf);	
	}else if(node->right == NULL){
		customInorderTraversal(node->left,prevleaf);	
	}else{
		customInorderTraversal(node->left,prevleaf);
		customInorderTraversal(node->right,prevleaf);	
	}
}

void createTree(TreeNode* node){
	TreeNode* h;
    h = node;
    h->left = new TreeNode(2);
	h->right = new TreeNode(3);
    h->left->left = new TreeNode(4);
	h->left->right = new TreeNode(5);
	h->right->left = new TreeNode(6);
	h->right->right = new TreeNode(7);
	h->left->left->left = new TreeNode(8);
	h->left->left->right = new TreeNode(9);
}

void displayLeafs(TreeNode * rootnode){
	TreeNode* root = rootnode;
	while(root->left != NULL){
		cout<<root->val<<" ";
		root = root->left;
	}
	cout<<"\n Leaf Path : ";
	while(root != NULL){
		cout<<root->val<<" ";
		root = root->right;
	}
}

int main(){
	TreeNode* rootnode = (TreeNode*) new TreeNode(1);
    cout<<" Root : "<<rootnode->val;
    createTree(rootnode);
	TreeNode* prev = NULL;
	customInorderTraversal(rootnode,&prev);
	displayLeafs(rootnode);
	
}

Space complexity O(1). TimeComplexity O(n)

- Rudra September 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the Space complexity of your solution is O(N). Stack space of the function should also be considered.

- Anonymous September 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Check if (node==NULL) at the very beginning of the function.

Although its not a problem - but your function name is quite misleading - its not inorder traversal - some custom preorder traversal.

- Psycho October 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Traverse the tree in-order, while keeping track of the last encountered leaf node. During the traversal, set the right of last encountered leaf-node to the next encountered leaf-node.

Time-complexity : O(n)
Space-complexity : O(height-of-tree)

public static void populateRightOfLeaves(Node root) {
		populateRightOfLeaves(root, null);
	}

	private static Node populateRightOfLeaves(Node root, Node prev) {
		
		if (root == null) {
			return prev;
		}
		
		if (root.left() == null && root.right() == null) { // if leaf
			if (prev != null) {
				prev.setRight(root);
			}
			return root;
		}
		
		prev = populateRightOfLeaves(root.left(), prev);
		return populateRightOfLeaves(root.right(), prev);
	}

- prasanth September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int fun(struct node *root)
{
int l,r;
static int flag=0;
static struct node *head = NULL;

if(root == NULL)
{
return 0;

}
l = fun(root->right);
r = fun(root->left);

if((l ==0) && (r ==0))
{
if(flag ==0)
{
flag =1;
head = root;
//printf("\n first head = %d\n", head->data);
}
else
{
root->right = head;
//printf("\n head = %d\n", root->data);
head = root;
}
return 1;
}
else
return 1;
}

- Damodar September 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void SetLeafRight(struct Node *&node,struct Node *&prev)
{
if(!node)
return;
SetLeafRight(node->left,prev);
SetLeafRight(node->right,prev);
if(!node->left && !node->right)
{
if(prev)
prev->right=node;
prev=node;
prev->right=NULL;
}
}

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

do reverse inorder traversal

void SetLeafRighttonextLeaf(struct Node *node)
{
static struct Node * next = NULL;
if(!node)
return;
SetLeafRight(node->right);
if(node->left == NULL && node->right == NULL)
{
node->right = next;
next = node;
}
SetLeafRight(node->left,prev);
}
}

- Ashsih September 19, 2013 | 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