Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8882947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:41:58+00:00 2026-06-14T20:41:58+00:00

I want to print tree in zigzag order. Here is my code but it

  • 0

I want to print tree in zigzag order. Here is my code but it is not running. Can Someone point out the mistake ?
I have created two stacks to do the same, my algorithm terminates when two of them are empty, if any of them contains some nodes then these are printed and its childrens are pushed in the other stack. This process continues till both stacks become empty.

#include<stdio.h>
#define MAX_SIZE 100

struct TreeNode{
      int value;
      struct TreeNode *left;
      struct TreeNode *right;
};


struct TreeNode stackA[MAX_SIZE]; 
struct TreeNode stackB[MAX_SIZE];

int topA = 0;
int topB = 0;


void push(struct TreeNode stack[], struct TreeNode *node, int *top){
     if(*top > MAX_SIZE){
             printf("stack is full\n");
     }
     else{
          stack[*top] = *node;
          *top = *top +1;
     }
     return;
}

struct TreeNode* pop(struct TreeNode stack[], int *top){
     if(*top == 0){
             printf("stack is empty\n");
               return NULL;
     }
     else{
          struct TreeNode *tmp = stack + *top;
          *top = *top - 1;
          return tmp;
     }


}

int isEmpty(int *top){
        if(*top == 0){
                return 1;
        }

        return 0;
}
void printZigZag(struct TreeNode* root){
     push(stackA,  root,&topA);
     printf("%d\n", topA);
     while(!isEmpty(&topA) || !isEmpty(&topB)){
        while(!isEmpty(&topA)){
           struct TreeNode* temp = pop(stackA,&topA);
           printf("%d", temp->value);
           if(temp->left) push(stackB,temp->left,&topB);
           if(temp->right) push(stackB,temp->right,&topB);
           printf("%d %d",topA,topB);
           return;
       }
       while(!isEmpty(&topB)){
           struct TreeNode *temp = pop(stackB,&topB);
           printf("%d", temp->value);
           if(temp->right) push(stackA,temp->right,&topA);
           if(temp->left) push(stackA,temp->left,&topB);
      }
   }
}


int main(){
    struct TreeNode* root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    root->value = 5;
    root->left = NULL;
    root->right = NULL;

    struct TreeNode* first = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    first->value = 15;
    first->left = NULL;
    first->right = NULL;
    root->left = first;


    struct TreeNode* second = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    second->value = 235;
    second->left = NULL;
    second->right = NULL;
    root->right = second;


    struct TreeNode *third = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    third->value = 45;
    third->left = NULL;
    third->right = NULL;
    first->left = third;


    struct TreeNode *fourth = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    fourth->value = 55;
    fourth->left = NULL;
    fourth->right = NULL;
    first->right = fourth;

    printZigZag(root);

    system("pause");
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:41:59+00:00Added an answer on June 14, 2026 at 8:41 pm

    I’ve check your code. The biggest mistake your have made is that the Pop function is implemented by error. The value of *pop should be decreased before you get the top value of the stack. Another mistake is that if(temp->left) push(stackA,temp->left,&topB) should be changed to if(temp->left) push(stackA,temp->left,&topA). Below is the code works, I change some small places to fit for c++.

    #include<cstdio>
    #define MAX_SIZE 100
    
    struct TreeNode{
          int value;
          struct TreeNode *left;
          struct TreeNode *right;
    };
    
    
    struct TreeNode stackA[MAX_SIZE]; 
    struct TreeNode stackB[MAX_SIZE];
    
    int topA = 0;
    int topB = 0;
    
    
    void push(struct TreeNode stack[], struct TreeNode *node, int *top){
         if(*top > MAX_SIZE){
                 printf("stack is full\n");
         }
         else{
              stack[*top] = *node;
              *top = *top +1;
         }
         return;
    }
    
    struct TreeNode* pop(struct TreeNode stack[], int *top){
         if(*top == 0){
                 printf("stack is empty\n");
                   return NULL;
         }
         else{
              *top = *top - 1; 
             struct TreeNode *tmp = stack +*top;         
              return tmp;
         }
    
    
    }
    
    int isEmpty(int *top){
            if(*top == 0){
                    return 1;
            }
    
            return 0;
    }
    void printZigZag(struct TreeNode* root){
         push(stackA,  root,&topA);
         while(!isEmpty(&topA) || !isEmpty(&topB)){
            while(!isEmpty(&topA)){
               struct TreeNode* temp = pop(stackA,&topA);
               printf("%d\n", temp->value);
               if(temp->left) push(stackB,temp->left,&topB);
               if(temp->right) push(stackB,temp->right,&topB);
           }
           while(!isEmpty(&topB)){
               struct TreeNode *temp = pop(stackB,&topB);
               printf("%d\n", temp->value);
               if(temp->right) push(stackA,temp->right,&topA);
               if(temp->left) push(stackA,temp->left,&topA);
          }
       }
    }
    
    int main(){
        struct TreeNode* root = new TreeNode();
        root->value = 5;
        root->left = NULL;
        root->right = NULL;
    
        struct TreeNode* first =new TreeNode();
        first->value = 15;
        first->left = NULL;
        first->right = NULL;
        root->left = first;
    
    
        struct TreeNode* second = new TreeNode();
        second->value = 235;
        second->left = NULL;
        second->right = NULL;
        root->right = second;
    
    
        struct TreeNode *third = new TreeNode();
        third->value = 45;
        third->left = NULL;
        third->right = NULL;
        first->left = third;
    
    
        struct TreeNode *fourth = new TreeNode();
        fourth->value = 55;
        fourth->left = NULL;
        fourth->right = NULL;
        first->right = fourth;
    
        printZigZag(root);
        return 0;
    }
    

    Hope it helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to print just the HTTP status code for a web page retrieved
I want to print something while my button is pressed (not after it is
I have a structure for a tree and I want to print the tree
here is post my code:this is no the entire code but enough to explain
I want to remove stop words. Here is my code import nltk from nltk.corpus
While printing Binary Search Tree(BST) using recursive function (pre-order). I need to print all
so i want to make a code, that creates a binary tree, that holds
So i want to make a code, that creates a binary tree, that holds
My C++ program creates a binary search tree. I know how to print out
I have this code about binary search tree , I want Calculated efficancy for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.