The binary tree above has a height of 2 and has 4 leaves. You are going to pass down a ball from the root (the topmost node). Every node other than the leaves has a switch. If a node is switched off , the ball will pass to its left child; if a node is switched on , the ball will pass to its right child. After passing the ball, the switch will toggle (if it's on it becomes off and vice versa). Initially, every node is switched off.
Chris built a similar tree of height with the same indexing. Which leaf will contain the ball?
Note : the indexes of the leaves should be in the range .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
By some observation, we can find a pattern. The algorithm is as follow :
x
is the index of the node that contains the ball.With this algorithm, we can still compute the final position of the ball by hand.
The desired answer is 4 5 2 .