If you support an ArrayList (e.g. node_list) to track the number of branch branches from the current node tree, you can traverse the tree from the root until you find a node that has an empty node_list. This way you can identify the leaf nodes of the tree. A recursive approach will work for this case. I have not tested the code, but I believe that this should work for what you requested:
If you are building something similar to the class below to build your tree:
class Node { String data; ArrayList<Node> node_list;}
The following recursive function might be what you are looking for:
public void traverse_tree(Node n){ if(n.node_list.isEmpty()){ System.out.print(n.data); } else{ for(Node current_node:n.node_list){ traverse_tree(current_node); } System.out.println(n.data); } }
In fact, you look at the depth after ordering. The first tree walk.
source share