Added debug function to flatten tree into array.