public static boolean isValid( Node node ) { // A null node is valid 🙂 if( node == null ) { return true; } // Check that this is not a root node (which we don't need to validate) if( node.parent != null ) { // Determine if this is a left child or right child. if( node == node.parent.left ) { return node.data < node.parent.data; } else { return node.data > node.parent.data; } } // Continue with the rest of left and right subtrees. return isValid( node.left ) && isValid( node.right ); }
Home » Binary Trees(We Are Computer Tree & NOT Free !) » Validating a Tree