## Amazon Interview Question for SDE-2s

Country: India
Interview Type: Written Test

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

``````private static void printPaths(BinaryTreeNode root, int[] path, int pathLen) {
if(root==null)
return;
path[pathLen]=root.getData();
pathLen++;

if(root.getLeft()==null && root.getRight()==null)
{
printArray(path,pathLen);
}
else
{
printPaths(root.getLeft(),path,pathLen);
printPaths(root.getRight(),path,pathLen);
}

}

private static void printArray(int[] path, int pathLen) {
int sum=0;
for(int i=0;i<pathLen;i++)
{
sum+=path[i];
System.out.print(path[i]+ "->");
}
totalsum+=sum;
System.out.println("  "+sum);
}``````

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

``````//in js
//here node is the at which you want to take the sum of paths.
Tree.prototype.pathSumRecur = function (node){
if (node == undefined){
return [0, 0];
}else if (node.lNode == undefined && node.rNode == undefined){
return [node.n, 1];
} else {
var lData = this.pathSumRecur(node.lNode);
var rData = this.pathSumRecur(node.rNode);
return [lData[0] + rData[0] + node.n * (lData[1] + rData[1]), lData[1] + rData[1]]
}
}``````

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

``````int Count(Node *head, int num)
{
int sum = 0;
}

}

sum = num * 10 + head->data;
}

return sum;
}``````

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

In Java.

Also, create the required Class/Object named Node.

``````int recursivelyFindTreePathSums (Node currentNode, int parentSumSoFar) {

if (currentNode.getLeftNode() != null && currentNode.getRightNode() != null)
return recursivelyFindTreePathSums(currentNode.getLeftNode(), parentSumSoFar * 10 + currentNode.getValue()) + recursivelyFindTreePathSums(currentNode.getRightNode(), parentSumSoFar * 10 + currentNode.getValue());

else if (currentNode.getLeftNode() != null)
return recursivelyFindTreePathSums(currentNode.getLeftNode(), parentSumSoFar * 10 + currentNode.getValue());

else if (currentNode.getRightNode() != null)
return recursivelyFindTreePathSums(currentNode.getRightNode(), parentSumSoFar * 10 + currentNode.getValue());

else
return parentSumSoFar * 10 + currentNode.getValue();
}``````

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.