@JZL003 solution is great, but parentFolder is not actually used, so code could be simplified further, such as:
function recurseFolder(myFolder) {
var childfolders = myFolder.getFolders(); // get the list of sub folders in the root of that folder
while (childfolders.hasNext()) { // count the sub folders
recurseFolder(childfolders.next());
}
var hasFile = myFolder.getFiles().hasNext(); // get the list of files in the root of that folder
var hasFolder = myFolder.getFolders().hasNext(); // get the list of files in the root of that folder
if (!hasFile && !hasFolder) {
myFolder.setTrashed(true);
}
}