def subtree(s, tree): """ Returns whether the given post order traversal index sequence represents a subtree or not :type s: list :type tree: Node :return: result :rtype: bool """ first_node_index = s[0] last_node_index = s[-1] # Neglecting explicitly the '_' characters if '_' in s: return False # Checking whether the first node is a leaf is_non_leaf = Node.has_children(tree, first_node_index) if is_non_leaf == -1: return False # Checking the left most descendant of the root of the subtree, is equal to the first node lmd = Node.lmd_subtree(tree, last_node_index) if not lmd: return False else: return not is_non_leaf and lmd == first_node_index