# Printing The Longest Path from Root to Leaf in Binary Tree [duplicate]

This question already has an answer here:

I am stumped as to how to print the longest path from the root of a binary tree to a leaf, essentially traversing the height of the tree. I've got the following for finding the height of a binary tree:

if (root == null) {
return -1;
}

return 1 + (Math.max(heightOfTree(root.leftNode), heightOfTree(root.rightNode)));


This easily finds the height of the tree but doesn't lend itself to actually figuring out which nodes should be printed out. I could go about finding the nodes's value that is at the leaf, and then do a find for that particular node, but that seems inefficient.

        A
B   C
D  E F  G
H


For example, the height of this binary tree is 3, and I would ideally like to know the path to take, in this case it would be A, C, G, H. I know that I most likely need to keep an array for the path, but I just can't seem to find a solution that isn't terribly inefficient.

## marked as duplicate by Yuval Filmus, frafl, J.-E. Pin, Luke Mathieson, David RicherbyOct 28 '13 at 20:58

• Related, if not a duplicate? – Juho Sep 30 '13 at 9:37

Though I don't believe this is the most efficient solution to the problem, it definitely works and I figured I would share it here and see if anyone had any feedback.

private static void printMain(BTNode node, String[] path, int arrayCounter) {
if (node == null) {
return;
}

path[arrayCounter++] = node.data;

if (node.leftNode == null && node.rightNode == null) {
printArray(path);
}
else {
printMain(node.leftNode, path, arrayCounter);
printMain(node.rightNode, path, arrayCounter);
}
}


This print function adds each node's value to an array until it reaches a node that has a left and right child that is null. This means it has reached the end and the array now has a path from the root node to a leaf node. It's not necessarily the longest path though. Here in the printArray function, it checks to see if the last element of the array is null.

Since the array is initialized to only be as large as the height found in the algorithm in my original question, if the last element is null this is not the longest path. If it is we print.

private static void printArray(String[] path) {
if (path[max] != null) {
for (String node : path) {
System.out.print(node + ", ");
}
System.out.print("\n");
}
}


The added benefit of this approach is that it will print each and every path that is the longest. Meaning if there were 3 nodes that are of height 3, each of the 3 would be printed as opposed to just the one. I still think it could be done more efficiently but this is certainly one solution.

Hint: Suppose we know that there is a longest path going through child X. If we ask X for a longest path from X to a leaf, would it help us to find a longest path from us to a leaf?

(This solution uses $O(1)$ memory, apart from the stack, if you don't care about printing the path backwards.)

• That would definitely help us find it, but I struggle to figure out how we would know that the longest path goes through a child X. I will think about it. Appreciate the hint. – KJ3 Sep 30 '13 at 0:56
• @Yuval filmus I didn't understand your hint but I think we can simply find out last node and then just a array to find out the parents until we reach the root. In your hint we can't know beforehand which nodes will be part of longest path until we traverse that node and successive nodes. – noman pouigt Feb 17 '17 at 3:46

A very simple solution is to augment the data structure so each node has a pointer to its parent. When you find the deepest leaf, then just follow parent pointers to the root to retrieve the path you want.