## Amazon Interview Question for SDE-2s

Country: United States

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

``````{
int data;
struct node *left;
struct node *right;
};

struct node *Node(int data)
{
struct node *tmp = (struct node *)malloc(sizeof(struct node));
tmp->data = data;
tmp->left = tmp->right = NULL;
}

int printlevel(struct node *tree, int level)
{
if ( tree == NULL)
return 0;

if (level == 1)
{
printf("%d  ", tree->data);
return 1;
}

int left = printlevel(tree->left,level-1);
int right = printlevel(tree->right, level-1);

return ( left || right );
}
int main()
{

struct node *tree=Node(15);
tree->left = Node(10);
tree->right=Node(20);
tree->left->left=Node(8);
tree->left->right=Node(12);
tree->right->left=Node(16);
tree->right->right=Node(25);

int level = 1;
while(printlevel(tree,level))
level++;

printf("\n");
return 0;
}``````

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

``````class Node:
def __init__(self, key=None, left=None, right=None):
self.key = key
self.left = left
self.right = right

def printLevel(root, level):
# base case
if root is None:
return False

if level == 1:
print(root.key, end=' ')

return True

left = printLevel(root.left, level - 1)
right = printLevel(root.right, level - 1)

return left or right

def levelOrderTraversal(root):
level = 1

while printLevel(root, level):
level = level + 1

if __name__ == '__main__':
root = Node(15)
root.left = Node(10)
root.right = Node(20)
root.left.left = Node(8)
root.left.right = Node(12)
root.right.left = Node(16)
root.right.right = Node(25)

levelOrderTraversal(root)``````

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

``````class Node:
def __init__(self, key=None, left=None, right=None):
self.key = key
self.left = left
self.right = right

def printLevel(root, level):

if root is None:
return False

if level == 1:
print(root.key, end=' ')
return True

left = printLevel(root.left, level - 1)
right = printLevel(root.right, level - 1)

return left or right

def levelOrderTraversal(root):
level = 1
while printLevel(root, level):
level = level + 1

if __name__ == '__main__':
root = Node(15)
root.left = Node(10)
root.right = Node(20)
root.left.left = Node(8)
root.left.right = Node(12)
root.right.left = Node(16)
root.right.right = Node(25)

levelOrderTraversal(root)``````

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

level order using dfs
recursive : recursively call left child first and right child next while adding visited nodes to result.
time O(n)
space O(n)
*/

``````class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();

levelOrderUtil(root, 0, result);

return result;
}

private static void levelOrderUtil(TreeNode node, int level, List<List<Integer>> result) {
if (node == null) {
return;
}

if (result.size() < level + 1) {
}

levelOrderUtil(node.left, level + 1, result);
levelOrderUtil(node.right, level + 1, result);

return;
}
}``````

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

/*
iterative : use a stack
time O(n)
space O(n)
*/

``````class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();

levelOrderUtil(root, result);

return result;
}

private void levelOrderUtil(TreeNode node, List<List<Integer>> result) {
if (node == null) {
return;
}

Stack<Node> s = new Stack<>();
Node temp = new Node(node, 0);
s.push(temp);

while (!s.empty()) {
temp = s.pop();

if (result.size() < temp.level + 1) {
}

if (temp.tn.right != null) {
s.push(new Node(temp.tn.right, temp.level + 1));
}
if (temp.tn.left != null) {
s.push(new Node(temp.tn.left, temp.level + 1));
}
}

return;
}

class Node {
TreeNode tn;
int level;

public Node(TreeNode tn, int level) {
this.tn = tn;
this.level = level;
}
}
}``````

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

public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
levelHelper(res, root, 0);
return res;
}

public void levelHelper(List<List<Integer>> res, TreeNode root, int height) {
if (root == null) return;
if (height >= res.size()) {
}
levelHelper(res, root.left, height+1);
levelHelper(res, root.right, height+1);
}

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.

### 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.