Check if the given Prufer sequence is valid or not | Function that returns true if given Prufer sequence is valid ; Iterate in the Prufer sequence ; If out of range ; Driver code
function isValidSeq ( a , n ) { var nodes = n + 2 ; for ( var i = 0 ; i < n ; i ++ ) { if ( a [ i ] < 1 a [ i ] > nodes ) return false ; } return true ; } var a = [ 4 , 1 , 3 , 4 ] ; var n = a . length ; if ( isValidSeq ( a , n ) ) document . write ( " " ) ; else document . write ( " " ) ;