Amazon Interview Question
SDE-3sCountry: United States
Interview Type: In-Person
public static int maxDistance(final Node node, final int currentMax, int max) {
if ((node.getLeftNode() == null) && (node.getRightNode() == null)) {
if (currentMax > max) {
max = currentMax;
}
} else {
if (node.getLeftNode() != null) {
max = maxDistance(node.getLeftNode(), currentMax + 1, max);
} else if (node.getRightNode() != null) {
max = maxDistance(node.getRightNode(), currentMax + 1, max);
}
}
return max;
}
public static int maxDistance(final Node node, final int currentMax, int max) {
if ((node.getLeftNode() == null) && (node.getRightNode() == null)) {
if (currentMax > max) {
max = currentMax;
}
} else {
if (node.getLeftNode() != null) {
max = maxDistance(node.getLeftNode(), currentMax + 1, max);
} else if (node.getRightNode() != null) {
max = maxDistance(node.getRightNode(), currentMax + 1, max);
}
}
return max;
}
public static int maxDistance(final Node node, final int currentMax, int max) {
if ((node.getLeftNode() == null) && (node.getRightNode() == null)) {
if (currentMax > max) {
max = currentMax;
}
} else {
if (node.getLeftNode() != null) {
max = maxDistance(node.getLeftNode(), currentMax + 1, max);
} else if (node.getRightNode() != null) {
max = maxDistance(node.getRightNode(), currentMax + 1, max);
}
}
return max;
}
- montu March 09, 2017