Find the Alpha Score of the Given Steps ( Using BST ) | Structure of a node ; Function to calculate and return the Alpha Score of the journey ; Traverse left subtree ; Calculate the alpha score of the current step ; Update alpha score of the journey ; Traverse right subtree ; Return ; Function to construct a BST from the sorted array [ ] arr ; Insert root ; Construct left subtree ; Construct right subtree ; Return root ; Driver Code ; Sort the array ; Construct BST from the sorted array
class Node { constructor ( data ) { this . data = data ; this . left = null ; this . right = null ; } } var root = null ; function AlphaScore ( ) { root = null ; } var sum = 0 , total_sum = 0 ; var mod = 1000000007 ; function getAlphaScore ( node ) { if ( node . left != null ) getAlphaScore ( node . left ) ; sum = ( sum + node . data ) % mod ; total_sum = ( total_sum + sum ) % mod ; if ( node . right != null ) getAlphaScore ( node . right ) ; return total_sum ; } function constructBST ( arr , start , end , root ) { if ( start > end ) return null ; var mid = parseInt ( ( start + end ) / 2 ) ; if ( root == null ) root = new Node ( arr [ mid ] ) ; root . left = constructBST ( arr , start , mid - 1 , root . left ) ; root . right = constructBST ( arr , mid + 1 , end , root . right ) ; return root ; } var arr = [ 10 , 11 , 12 ] ; var length = arr . length ; arr . sort ( ) ; var root = null ; root = constructBST ( arr , 0 , length - 1 , root ) ; document . write ( getAlphaScore ( root ) ) ;