static bool isBinaryTree(NodeTree root) { // traverse the tree in inorder fashion and // keep track of prev node if (root != null) { if (!isBinaryTree(root.Left)) { return(false); } // Allows only distinct valued nodes if (prev != null && root.Data <= prev.Data) { return(false); } prev = root; return(isBinaryTree(root.Right)); } return(true); }
static NodeTree Insert(NodeTree root, int data) { if (root == null) { return(new NodeTree(data)); } else { NodeTree cur; if (data <= root.Data) { cur = Insert(root.Left, data); root.Left = cur; } else { cur = Insert(root.Right, data); root.Right = cur; } return(root); } }
static bool CheckBinaryTree(NodeTree root) { return(isBinaryTree(root)); }
public NodeTree(int data) { this.Data = data; Left = Right = null; }