code: def _sort_tree_leaves(g, root):
      """For each node, sort its child nodes based on the height of the nodes.
      Return the leaf nodes of the tree after sorting.
      """
  
      def _get_height(root):
          return g.nodes[root]["height"]
  
      def _get_sorted_leaves(root):
          children = list(g.successors(root))
          if not children:
              return [root]
          child_heights = [_get_height(x) for x in children]
          order = sorted(
              range(len(children)), key=lambda x: child_heights[x]
          )
          ret = []
          for co in order:
              cr = children[co]
              ret += _get_sorted_leaves(cr)
  
          return ret
  
      return _get_sorted_leaves(root)
  
imports: import networkx
  import collections
  import time
  import copy
  from caffe2.python import workspace, core
  from caffe2.proto import caffe2_pb2
  import enum
  import logging
  import caffe2.python._import_c_extension
label_desc: Use image node_id 12 for calling a global function with example usage: _sort_tree_leaves(g, root) and returns: _get_sorted_leaves, g, ret, list