Connection Status:
Competition Arena > ConstructBST
SRM 319 · 2006-09-18 · by NeverMore · Advanced Math, Recursion
Class Name: ConstructBST
Return Type: long
Method Name: numInputs
Arg Types: (vector<int>)
Problem Statement

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.

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, 2, 3}
Returns: 2
2)
{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.

3)
{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".

4)
{1,2,4,8,16,32,64,128,256,512,1024,2048,4096}
Returns: 1

Submissions are judged against all 82 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class ConstructBST with a public method long long numInputs(vector<int> tree) · 82 test cases · 2 s / 256 MB per case

Submitting as anonymous