void () { DefaultMutableTreeNode root = new DefaultMutableTreeNode("0"); DefaultMutableTreeNode node = new DefaultMutableTreeNode("00"); root.add(node); node.add(new DefaultMutableTreeNode("000")); node.add(new DefaultMutableTreeNode("001")); root.add(new DefaultMutableTreeNode("01")); final ArrayList order = new ArrayList(); TreeUtil.traverseDepth(root, node1 -> { order.add(node1.toString()); return true; }); assertThat(order).containsExactly("0", "00", "000", "001","01"); }