code: def tree(height=3, is_perfect=False):
      
      _validate_tree_height(height)
      values = _generate_random_node_values(height)
      if is_perfect:
          return build(values)
  
      leaf_count = _generate_random_leaf_count(height)
      root = Node(values.pop(0))
      leaves = set()
  
      for value in values:
          node = root
          depth = 0
          inserted = False
  
          while depth < height and not inserted:
              attr = random.choice(('left', 'right'))
              if getattr(node, attr) is None:
                  setattr(node, attr, Node(value))
                  inserted = True
              node = getattr(node, attr)
              depth += 1
  
          if inserted and depth == height:
              leaves.add(node)
          if len(leaves) == leaf_count:
              break
  
      return root
docs: Generate a random binary tree and return its root node.
  
      :param height: Height of the tree (default: 3, range: 0 - 9 inclusive).
      :type height: int
      :param is_perfect: If set to True (default: False), a perfect binary tree
          with all levels filled is returned. If set to False, a perfect binary
          tree may still be generated by chance.
      :type is_perfect: bool
      :return: Root node of the binary tree.
      :rtype: binarytree.Node
      :raise binarytree.exceptions.TreeHeightError: If height is invalid.
  
      **Example**:
  
      .. doctest::
  
          >>> from binarytree import tree
          >>>
          >>> root = tree()
          >>>
          >>> root.height
          3
  
      .. doctest::
  
          >>> from binarytree import tree
          >>>
          >>> root = tree(height=5, is_perfect=True)
          >>>
          >>> root.height
          5
          >>> root.is_perfect
          True
  
      .. doctest::
  
          >>> from binarytree import tree
          >>>
          >>> root = tree(height=20)  # doctest: +IGNORE_EXCEPTION_DETAIL
          Traceback (most recent call last):
           ...
          TreeHeightError: height must be an int between 0 - 9