-2

Given a binary tree, whose root is located a treasure, and whose internal nodes can contain a dragon or does not contain anything, you are asked to design an algorithm that tells us the leaf of the tree whose path to the root has the lowest number of dragons. In the event that there are multiple paths with the same number of dragons, the algorithm will return that which is more to the left of all them. To do this, implement a function which gets a binary tree whose nodes store integers:

  1. The root contains the integer 0, which represents the treasure.
  2. The internal nodes contain the integer 1 to indicate that the node there is a dragon or the integer 2 to indicate that there is no dragon.
  3. In each leaf stores an integer greater than or equal to 3 that cannot be repeated. and return the whole sheet to the path selected. The tree has at least one root node and a leaf node different from the root. For example, given the following tree (the second test case shown in the example), the algorithm return the integer 4.

I can not upload a picture of the tree of example, but someone tell me with words that I can do to go through all the branches, and to know which is the path with less dragons I'd appreciate it.

A greeting!

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
Alvaro
  • 47
  • 6

2 Answers2

3

You want to think about these problems recursively: if you're at a parent node with...

  • no children you must have no dragon and a node counter, and you consider yourself to have 0 dragons and be the best node: you'd tell your parent that if asked

  • a left branch and/or a right branch, then you ask your children for their dragon-count and which node they consider best, and IF the left node reports a lesser or equal dragon count...

    • you take your best-node and dragon-count from it, ELSE

    • you take your best-node and dragon-count from the right node

    then you add 1 to the dragon-count if your node's storing the integer 1

By starting that processing at the root node, you get the result for the entire tree.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
1

This is the first algorithm that comes to mind. Assuming that you have an array that stores the values in nodes node_value[NODE_NUM], where NODE_NUM is the number of nodes in your tree, and you store index of childs of each node with the arrays left[NODE_NUM] and right[NODE_NUM], and your root will have index root_index. We will store information about the number of dragons in the path to root in the array dragon[NODE_NUM] So the algorithm pseudocode is:

# the recursive function itself   
process(node_index):
    n_left <- 0
    if node_value[left[node_index]] = 1
        n_left <- 1
    n_right <- 0
    if node_value[right[node_index]] = 1
        n_right <- 1

    dragon[left[node_index]] <- dragon[node_index] + n_left
    dragon[right[node_index]] <- dragon[node_index] + n_right
    process(left[node_index])
    process(right[node_index])

# the number of dragons in path from root to root is, obviously, zero:
dragon[root_index] <- 0

# Call the function itself
process(root_index)        

After that, in dragon we will have the number of dragons in the way to root from every nodes in tree. Now, all you have to do is to loop through all nodes and find the node that is a leaf and that its values is minimal:

min <- infinity
node_min <- unknown
for each node:
    if node_value[node] >= 3:
        if dragon[node] < min:
            min <- dragon[node]
            node_min <- node

return node_min

Now, the node_min is the node that has least dragons in the path to root.

Chan Kha Vu
  • 9,834
  • 6
  • 32
  • 64