Check if the given Prufer sequence is valid or not | C ++ implementation of the approach ; Function that returns true if given Prufer sequence is valid ; Iterate in the Prufer sequence ; If out of range ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValidSeq ( int a [ ] , int n ) { int nodes = n + 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] < 1 a [ i ] > nodes ) return false ; } return true ; } int main ( ) { int a [ ] = { 4 , 1 , 3 , 4 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; if ( isValidSeq ( a , n ) ) cout << " Valid " ; else cout << " Invalid " ; return 0 ; }