Check if each internal node of a BST has exactly one child | Check if each internal node of BST has only one child ; Driver code
using System ; public class BinaryTree { bool hasOnlyOneChild ( int [ ] pre , int size ) { int nextDiff , lastDiff ; for ( int i = 0 ; i < size - 1 ; i ++ ) { nextDiff = pre [ i ] - pre [ i + 1 ] ; lastDiff = pre [ i ] - pre [ size - 1 ] ; if ( nextDiff * lastDiff < 0 ) { return false ; } ; } return true ; } public static void Main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; int [ ] pre = new int [ ] { 8 , 3 , 5 , 7 , 6 } ; int size = pre . Length ; if ( tree . hasOnlyOneChild ( pre , size ) == true ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }