This is an algorithm that I was given a while ago for a test and I couldn't figure it out. Any ideas?
You are given a recursive notation of a binary tree: each node of a tree is represented as a set of three elements:
- value of the node
- left subtree
- right subtree
So, a tree can be written as (value left_subtree right_subtree)
.
If a node doesn't exist then it is represented as an empty set: ()
.
Your task is to obtain a list of nodes, that are the most distant from the tree root, in the order from left to right.
In the notation of a node its value and subtrees are separated by exactly one space character.
Example:
// 2
// / \
// / \
// / \
// / \
// / \
// 7 5
// / \ \
// / \ \
// 2 6 9
// / \ /
// / \ /
// 5 11 4
tree = "(2 (7 (2 () ()) (6 (5 () ()) (11 () ()))) (5 () (9 (4 () ()) ())))"
treeBottom(tree) // Desired output: [5, 11, 4].