In general it's fine to require things inside functions, assuming the function in question is not necessarily going to be used, and the rest of your library doesn't need that dependency.
Your example wouldn't necessitate that, though. Based on the autoload cookie, you're intending that your library doesn't get loaded at all until my-mode is invoked, at ...
Command dirtree is not marked for autoloading so you need do (require 'dirtree) first.
Or follow instructions provided in dirtree commentary section:
Put the following into your ~/.emacs:
(autoload 'dirtree "dirtree" "Add directory to tree view" t)