TopCoder problem "ConstructBST" used in SRM 319 (Division I Level Two)



Problem Statement

    This statement contains images which must be viewed in the applet.

A Binary Search Tree (BST) is a tree data structure which obeys the following properties:

  • Each node has at most 2 children.
  • Each node except the root has exactly 1 parent.
  • There is exactly 1 root node. The root node does not have any parents.
  • If a node has a left child, all values in the left subtree are less than or equal to the value of the node itself.
  • If a node has a right child, all values in the right subtree are strictly greater than the value of the node itself.

A BST is constructed from a string of characters in the following way: The root node of the tree is assigned the value of the first character in the string. All subsequent characters are added as children of existing nodes subject to the above rules. Note that the tree is never altered in any other manner. In this problem, nodes are identified starting from the root node as 1, and following in order with nodes at the next level, etc. See the figure below for clarification.

You will be given the shape of a particular BST by specifying the identities of the nodes which constitute the tree, according to the above scheme. Note that you do not know the exact input string used to construct the given BST. You do know, however, that the first N uppercase letters were used to construct the given BST with N nodes. See the examples below for further clarification.

Create a class ConstructBST that contains a method numInputs. The method takes an int[] tree with a list of node numbers present in the tree and returns a long corresponding to the number of strings composed of the characters from 'A' to 'Z' that could have been used to produce the particular BST. Note that tree will always contain a root node numbered 1. Note also, that tree will represent a connected tree, that is, all nodes present in the tree will be reachable from the root node.

 

Definition

    
Class:ConstructBST
Method:numInputs
Parameters:int[]
Returns:long
Method signature:long numInputs(int[] tree)
(be sure your method is public)
    
 

Constraints

-tree will contain between 1 and 26 elements, inclusive.
-Each element of tree will be between 1 and 226-1, inclusive.
-Each element of tree will be distinct.
-tree will represent a connected tree.
-tree will contain a root node numbered 1.
 

Examples

0)
    
{1, 2}
Returns: 1

This input represents a BST of the following shape:

Using the characters {'A','B'}, only the string "BA" can generate a tree with the given shape.

1)
    
{1, 3, 6}
Returns: 1

This input represents a BST of the following shape:

Using the characters {'A','B','C'}, only the string "ACB" can generate a tree with the given shape.

2)
    
{1, 2, 5, 3, 4}
Returns: 8
Using the characters {'A','B','C','D','E'} the following 8 strings generate a tree with the given shape: "DBACE", "DBAEC", "DBEAC", "DEBAC", "DBCAE", "DBCEA", "DBECA", "DEBCA".
3)
    
{1, 2, 4, 6, 3}
Returns: 6
4)
    
{2, 4, 3, 62, 7, 15, 1, 31, 5, 14, 28, 57, 56, 114}
Returns: 96096

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=6714

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=9999&pm=6714

Writer:

NeverMore

Testers:

PabloGilberto , brett1479 , Olexiy , ged

Problem categories:

Advanced Math, Recursion