TutorialStudyMite

Level order traversal – Spiral form

Sstudymite1 min read
Beginner friendly

Track completion, mastery, and revision.

Level order traversal in Spiral form

We will traverse each level and store all the nodes at that level in a temporary vector. We will keep the current direction in a variable that will change after traversing each level.

#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* left;
Node* right;
Node(int data){
this->data = data;
left = right = NULL;
}
};
void levelOrderSpiral(Node* root, int state){
if(!root) return;

}
int main() {
/*
1
/    
2      3
/   \   /  
4     5 6    7
 root = new Node(1);
root->left = new Node(2);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right = new Node(3);
root->right->left = new Node(6);
root->right->right = new Node(7);
levelOrderSpiral(root, 0);
return 0;
}

Finished reading?

Was this helpful?

Your feedback shapes better tutorials for everyone.