Flatten a Binary Search Tree to convert the tree into a wave list in place only

  #include using namespace std;  struct TreeNode {      int data;    TreeNode* right;    TreeNode* left;          TreeNode(int data)    {        this->data = data;        this->left = NULL;        this->right = NULL;    }};  TreeNode* insertNode(int data,                     TreeNode* root){    if (root == NULL) {        TreeNode* node            = new TreeNode(data);        return node;    }    else if (data > root->data) {        root->right = insertNode(            data, root->right);    }    else if (data data) {        root->left = insertNode(            data, root->left);    }          return root;}  int countNodes(TreeNode* root){    if (root == NULL)        return 0;      else        return countNodes(root->left)               + countNodes(root->right) + 1;}  TreeNode* toWaveList(TreeNode* root){    int count = countNodes(root);    vector ans;          stack s1;          stack s2;      TreeNode *root1 = root, *root2 = root;    TreeNode *curr1 = NULL, *curr2 = NULL;              TreeNode* head = NULL;          TreeNode* temp = NULL;      int l = 0;    while (l++ < ceil(count / 2.0)) {                  while (root1) {            s1.push(root1);            root1 = root1->left;        }        curr1 = s1.top();        s1.pop();        root1 = curr1->right;                          while (root2) {            s2.push(root2);            root2 = root2->right;        }        curr2 = s2.top();        s2.pop();        root2 = curr2->left;                                  if (curr1->data == curr2->data) {            temp->right = curr1;            temp = temp->right;            break;        }                  if (head == NULL) {            head = curr1;            temp = curr1;              temp->right = curr2;                          temp = temp->right;        }        else {              temp->right = curr1;            temp = temp->right;              temp->right = curr2;            temp = temp->right;        }    }    temp->right = NULL;              temp = head;    while (temp) {        temp->left = NULL;        temp = temp->right;    }      return head;}  int main(){    TreeNode* root = NULL;    int tree[] = { 1, 0, 10 };    for (int i = 0; i < 3; i++) {        root = insertNode(tree[i], root);    }          TreeNode* head = toWaveList(root);    while (head) {        cout data right;    }      return 0;}