Waste less time on Facebook — follow Brilliant.
×

Trees

Whether you're working with a road map or just some numerical data, organizing data in trees allows for an efficient representation of connections and hierarchies.

Level 5

The following python code describes an algorithm for inserting values into a binary search tree.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Node:
    def __init__(self, val):
        self.left_child = None
        self.right_child = None
        self.data = val

def binary_insert(tree, val):
    # insert val at root if tree is empty
    if tree is None:
        tree = Node(val)
    else:
        # if val is less than current node, 
        # insert val into left subtree
        if (tree.data > val):
            tree.left_child = binary_insert(tree.left_child, val)
        # else, insert val into right subtree
        else:
            tree.right_child = binary_insert(tree.right_child, val)
    return tree

Using this algorithm, you create a new binary search tree B, and insert the following elements into it in this order:

1
4, 2, 1, 5, 6, 3, 7, 9, 8, 12, 10, 11, 13, 15, 14

After these operations, what is the sum of the data in the leaf nodes of B?

Details and assumptions

A leaf node is a node that has no children (no left_child or right_child).

In the following sequence of 30 integers, determine how many subsets (including the empty one) have a sum between -700 and 700 inclusive.

How many 16-digit integers are there such that the sum of the square of their digits is a square?

Examples And Test Cases

  • \(2283\) is a \(4\)-digit integer such that the sum of the square of its digits is a square. \(2^2 + 2^2 + 8^2 + 3^2 = 81 = 9^2 \).

  • There are \(332\) \(4\)-digit integers with the required property.

×

Problem Loading...

Note Loading...

Set Loading...