I was writing an 'Easy' Java solution was trying to find the bottleneck for 2ms / 1ms. I narrowed down to this.
This block gives me 75.84%:
if (node != null) {
path.add(node.val);
if (node.left == null && node.right == null) {
paths.add(pathAsString(path));
} else {
dfs(node.left, paths, new LinkedList<>(path));
dfs(node.right, paths, new LinkedList<>(path));
}
}This gives me 99.95%:
if (node != null) {
path.add(node.val);
if (node.left == null && node.right == null) {
paths.add(pathAsString(path));
} else {
dfs(node.left, paths, new LinkedList<>(path));
dfs(node.right, paths, new LinkedList<>(path));
}
}Removing the blank line gives me the increase in speed - but why? It shouldn't...