Amazon Interview Question | Next Greater Element

Given a complete binary tree, (input is given in array form), you need to return the next greater element (according to the tree) for some element k present in tree.

Input: array = [ 10 , 5 , 15, 2, 8, 12, 16],k=5

Explanation :

                     10
             5                  15
      2        8.         12.       16
	  
	  
	  for k = 5, next greater element is 8 according to the tree
	  

Please share your approaches!
P.S.: He was expecting O(logn) solution. I was thinking of some approach similar to that of we use while implementing heap in arrays.

Comments (8)