Hi I have the following problem. Given a Tree I want to be able to check the number of its nodes that have values higher than that of their root and nodes above them. i.e. if a node has a value of 11 it will should increase the counter as long as it is higher (or equal) than root and all the nodes above it (so all values up to root have to be lower or equal to 11). How to best achieve that?
thank you
Do a breadth first tree traversal. At the end of each level add the node values (for that level only) in a hash table, which is used to make your counter comparisons.
Playout:
hashTable = {}
hashTable = {2}
hashTable = {2, 1, 3}
.
.
.
count = 6 nodes greater or equal to all the nodes above them.