Level order traversal in spiral form (original) (raw)
Last Updated : 9 Apr, 2026
Given a binary tree and we have to find the spiral order traversal of the tree and return the list containing the elements.
**Spiral order Traversal: Starting from level 0 for root node, for all the even levels we print the node's value from right to left and for all the odd levels we print the node's value from left to right.
**Example:
**Input: root = [1, 2, 3, 7, 6, 5, 4]
**Output: [1, 2, 3, 4, 5, 6, 7]
**Explanation: Start with root (1), print level 0 (right to left), level 1 (left to right), and continue alternating.**Input: root = [1, 3, 2]
**Output: [1, 3, 2]
**Explanation: Start with root (1), print level 0 (right to left), then level 1 (left to right)**Input: root = [10, 20, 30, 40, 60]
**Output: [10, 20, 30, 60, 40]
**Explanation: Start with root (10), print level 0 (right to left), level 1 (left to right), and continue alternating.
Table of Content
- Using Recursion - O(n) Time and O(h) Space
- Using Two Stacks - O(n) Time and O(n) Space
- Using Deque - O(n) Time and O(n) Space
Using Recursion - O(n) Time and O(h) Space
The idea is to first calculate the height of the tree, then recursively traverse each level and print the level order traversal according to the current level being odd or even.
**Steps to solve the problem:
- Find the height
hof the binary tree. - Use a boolean flag variable
ltr(left-to-right), start asfalse. - For each level
,CallprintGivenLevel(root, level, ltr). - Toggle
ltrafter each level. - If node is **null → return.
- If
level == 1→ process the current node. - If
level > 1,Ifltr→ recurse **left → right otherwise → recurse **right → left. C++ `
#include #include using namespace std;
struct Node { int data; Node* left; Node* right;
Node(int data) {
this->data = data;
left = right = nullptr;
}};
int height(Node* node) { if (node == nullptr) return 0; int leftHeight = height(node->left); int rightHeight = height(node->right); return max(leftHeight, rightHeight) + 1; }
// Main recursive function that stores the // spiral traversal in vector res. void getLevel(Node* root, int level, bool ltr, vector& res) { if (root == nullptr) return; if (level == 1) res.push_back(root->data); else if (level > 1) { if (ltr) { getLevel(root->left, level - 1, ltr, res); getLevel(root->right, level - 1, ltr, res); } else { getLevel(root->right, level - 1, ltr, res); getLevel(root->left, level - 1, ltr, res); } } }
vector findSpiral(Node* root) { vector res; int h = height(root); bool ltr = false; for (int i = 1; i <= h; i++) { getLevel(root, i, ltr, res); ltr = !ltr; } return res; } void printSpiral(vector&res) { for(auto i:res) cout<<i<<" "; }
int main() { Node* root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(7); root->left->right = new Node(6); root->right->left = new Node(5); root->right->right = new Node(4);
vector<int> res = findSpiral(root);
printSpiral(res);
return 0;}
Java
import java.util.ArrayList;
// Class representing a node of the binary tree class Node { int data; Node left, right;
Node(int data) {
this.data = data;
left = right = null;
}}
class GfG {
// Function to calculate the height of the binary tree
static int height(Node node) {
if (node == null)
return 0;
int leftHeight = height(node.left);
int rightHeight = height(node.right);
// Height is max of left/right subtree + 1 (for current node)
return Math.max(leftHeight, rightHeight) + 1;
}
// Function to perform spiral (zig-zag) level order traversal
static ArrayList<Integer> findSpiral(Node root) {
ArrayList<Integer> result = new ArrayList<>();
int h = height(root); // Get height of tree
boolean leftToRight = false; // Direction flag
// Traverse each level
for (int level = 1; level <= h; level++) {
getLevel(root, level, leftToRight, result);
leftToRight = !leftToRight;
}
return result;
}
// Helper function to get nodes at a given level in desired order
static void getLevel(Node root, int level, boolean leftToRight,
ArrayList<Integer> result) {
if (root == null)
return;
if (level == 1) {
// If it's the current level, add node to result
result.add(root.data);
} else {
// Recur for left and right children in
// order based on direction
if (leftToRight) {
getLevel(root.left, level - 1, leftToRight, result);
getLevel(root.right, level - 1, leftToRight, result);
} else {
getLevel(root.right, level - 1, leftToRight, result);
getLevel(root.left, level - 1, leftToRight, result);
}
}
}
public static void printSpiral(ArrayList<Integer> res)
{
for (int val : res)
System.out.print(val + " ");
}
// Main function to test the spiral traversal
public static void main(String[] args) {
// Creating a binary tree
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(7);
root.left.right = new Node(6);
root.right.left = new Node(5);
root.right.right = new Node(4);
// Performing spiral traversal
ArrayList<Integer> res = findSpiral(root);
// Printing the result
printSpiral(res);
}}
Python
#include #include
class Node: def init(self, data): self.data = data self.left = None self.right = None
def height(node): if node is None: return 0 leftHeight = height(node.left) rightHeight = height(node.right) return max(leftHeight, rightHeight) + 1
def findSpiral(root): res = [] h = height(root) ltr = False for i in range(1, h + 1): getLevel(root, i, ltr, res) ltr = not ltr return res
Main recursive function that stores the
spiral traversal in list res.
def getLevel(root, level, ltr, res): if root is None: return if level == 1: res.append(root.data) elif level > 1: if ltr: getLevel(root.left, level - 1, ltr, res) getLevel(root.right, level - 1, ltr, res) else: getLevel(root.right, level - 1, ltr, res) getLevel(root.left, level - 1, ltr, res) def printSpiral(res): for x in res: print(x, end=' ') if name == 'main': root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(7) root.left.right = Node(6) root.right.left = Node(5) root.right.right = Node(4)
res = findSpiral(root)
printSpiral(res)C#
using System; using System.Collections.Generic;
class Node { public int data; public Node left, right;
public Node(int data) {
this.data = data;
left = right = null;
}}
class GfG { static int Height(Node node) { if (node == null) return 0; int leftHeight = Height(node.left); int rightHeight = Height(node.right); return Math.Max(leftHeight, rightHeight) + 1; }
static List<int> findSpiral(Node root) {
List<int> res = new List<int>();
int h = Height(root);
bool ltr = false;
for (int i = 1; i <= h; i++) {
getLevel(root, i, ltr, res);
ltr = !ltr;
}
return res;
}
static void getLevel(Node root, int level, bool ltr, List<int> res) {
if (root == null) return;
if (level == 1)
res.Add(root.data);
else if (level > 1) {
if (ltr) {
getLevel(root.left, level - 1, ltr, res);
getLevel(root.right, level - 1, ltr, res);
} else {
getLevel(root.right, level - 1, ltr, res);
getLevel(root.left, level - 1, ltr, res);
}
}
}
public static void printSpiral(List<int> res)
{
foreach (int x in res)
Console.Write(x + " ");
}
public static void Main() {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(7);
root.left.right = new Node(6);
root.right.left = new Node(5);
root.right.right = new Node(4);
List<int> res = findSpiral(root);
printSpiral(res);
}}
JavaScript
class Node { constructor(data) { this.data = data; this.left = this.right = null; } }
function height(node) { if (node === null) return 0; let leftHeight = height(node.left); let rightHeight = height(node.right); return Math.max(leftHeight, rightHeight) + 1; }
function findSpiral(root) { let res = []; let h = height(root); let ltr = false; for (let i = 1; i <= h; i++) { getLevel(root, i, ltr, res); ltr = !ltr; } return res; }
function getLevel(root, level, ltr, res) { if (root === null) return; if (level === 1) res.push(root.data); else if (level > 1) { if (ltr) { getLevel(root.left, level - 1, ltr, res); getLevel(root.right, level - 1, ltr, res); } else { getLevel(root.right, level - 1, ltr, res); getLevel(root.left, level - 1, ltr, res); } } } function printSpiral(res) { console.log(res.join(' ')); } // Driver Code const root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(7); root.left.right = new Node(6); root.right.left = new Node(5); root.right.right = new Node(4);
const res = findSpiral(root); printSpiral(res);
`
Using Two Stacks - O(n) Time and O(n) Space
The idea is to use t**wo stacks. One stack **s1 is used to traverse the current level and the other stack **s2 is used to store the nodes of next level.
**Steps to solve the problem:
- Initialize two stacks
s1ands2, push root intos1. - While either stack is not empty:
- From
s1: pop nodes, add to result, push **right → left children intos2. - From
s2: pop nodes, add to result, push **left → right children intos1. - Directions alternate naturally:
s1handles right-to-left,s2handles left-to-right. C++ `
#include #include #include using namespace std;
struct Node { int data; Node *left, *right;
Node(int val) {
data = val;
left = right = nullptr;
}};
vector findSpiral(Node* root) { vector res; if (root == nullptr) return res;
stack<Node*> s1; // Current level
stack<Node*> s2; // Next level
s1.push(root);
while (!s1.empty() || !s2.empty()) {
// Print nodes of current level from s1
// and push nodes of next level to s2
while (!s1.empty()) {
Node* temp = s1.top();
s1.pop();
res.push_back(temp->data);
if (temp->right)
s2.push(temp->right);
if (temp->left)
s2.push(temp->left);
}
// Print nodes of current level from s2
// and push nodes of next level to s1
while (!s2.empty()) {
Node* temp = s2.top();
s2.pop();
res.push_back(temp->data);
if (temp->left)
s1.push(temp->left);
if (temp->right)
s1.push(temp->right);
}
}
return res;}
// New function to print spiral order from result vector void printSpiral(vector& res) { for (int x : res) cout << x << " "; cout << endl; }
int main() { Node* root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(7); root->left->right = new Node(6); root->right->left = new Node(5); root->right->right = new Node(4);
// First get result
vector<int> res = findSpiral(root);
// Then pass it to printSpiral
printSpiral(res);
return 0;}
Java
import java.util.*;
class Node { int data; Node left, right;
Node(int val) {
data = val;
left = right = null;
}}
public class SpiralTraversal {
static List<Integer> findSpiral(Node root) {
List<Integer> res = new ArrayList<>();
if (root == null)
return res;
Stack<Node> s1 = new Stack<>(); // Current level
Stack<Node> s2 = new Stack<>(); // Next level
s1.push(root);
while (!s1.isEmpty() || !s2.isEmpty()) {
// Print nodes of current level from s1
// and push nodes of next level to s2
while (!s1.isEmpty()) {
Node temp = s1.pop();
res.add(temp.data);
if (temp.right != null)
s2.push(temp.right);
if (temp.left != null)
s2.push(temp.left);
}
// Print nodes of current level from s2
// and push nodes of next level to s1
while (!s2.isEmpty()) {
Node temp = s2.pop();
res.add(temp.data);
if (temp.left != null)
s1.push(temp.left);
if (temp.right != null)
s1.push(temp.right);
}
}
return res;
}
// New function to print spiral order from result list
static void printSpiral(List<Integer> res) {
for (int x : res)
System.out.print(x + " ");
System.out.println();
}
public static void main(String[] args) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(7);
root.left.right = new Node(6);
root.right.left = new Node(5);
root.right.right = new Node(4);
// First get result
List<Integer> res = findSpiral(root);
// Then pass it to printSpiral
printSpiral(res);
}}
Python
class Node: def init(self, val): self.data = val self.left = None self.right = None
def findSpiral(root): res = [] if root is None: return res
s1 = [] # Current level
s2 = [] # Next level
s1.append(root)
while s1 or s2:
# Print nodes of current level from s1
# and push nodes of next level to s2
while s1:
temp = s1.pop()
res.append(temp.data)
if temp.right:
s2.append(temp.right)
if temp.left:
s2.append(temp.left)
# Print nodes of current level from s2
# and push nodes of next level to s1
while s2:
temp = s2.pop()
res.append(temp.data)
if temp.left:
s1.append(temp.left)
if temp.right:
s1.append(temp.right)
return resNew function to print spiral order from result list
def printSpiral(res): for x in res: print(x, end=" ") print()
if name == "main": root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(7) root.left.right = Node(6) root.right.left = Node(5) root.right.right = Node(4)
# First get result
res = findSpiral(root)
# Then pass it to printSpiral
printSpiral(res)C#
using System; using System.Collections.Generic;
class Node { public int data; public Node left, right;
public Node(int val) {
data = val;
left = right = null;
}}
class SpiralTraversal { static List findSpiral(Node root) { List res = new List(); if (root == null) return res;
Stack<Node> s1 = new Stack<Node>(); // Current level
Stack<Node> s2 = new Stack<Node>(); // Next level
s1.Push(root);
while (s1.Count > 0 || s2.Count > 0) {
// Print nodes of current level from s1
// and push nodes of next level to s2
while (s1.Count > 0) {
Node temp = s1.Pop();
res.Add(temp.data);
if (temp.right != null)
s2.Push(temp.right);
if (temp.left != null)
s2.Push(temp.left);
}
// Print nodes of current level from s2
// and push nodes of next level to s1
while (s2.Count > 0) {
Node temp = s2.Pop();
res.Add(temp.data);
if (temp.left != null)
s1.Push(temp.left);
if (temp.right != null)
s1.Push(temp.right);
}
}
return res;
}
// New function to print spiral order from result list
static void printSpiral(List<int> res) {
foreach (int x in res)
Console.Write(x + " ");
Console.WriteLine();
}
static void Main() {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(7);
root.left.right = new Node(6);
root.right.left = new Node(5);
root.right.right = new Node(4);
// First get result
List<int> res = findSpiral(root);
// Then pass it to printSpiral
printSpiral(res);
}}
JavaScript
class Node { constructor(val) { this.data = val; this.left = null; this.right = null; } }
function findSpiral(root) { let res = []; if (root === null) return res;
let s1 = []; // Current level
let s2 = []; // Next level
s1.push(root);
while (s1.length > 0 || s2.length > 0) {
// Print nodes of current level from s1
// and push nodes of next level to s2
while (s1.length > 0) {
let temp = s1.pop();
res.push(temp.data);
if (temp.right)
s2.push(temp.right);
if (temp.left)
s2.push(temp.left);
}
// Print nodes of current level from s2
// and push nodes of next level to s1
while (s2.length > 0) {
let temp = s2.pop();
res.push(temp.data);
if (temp.left)
s1.push(temp.left);
if (temp.right)
s1.push(temp.right);
}
}
return res;}
// New function to print spiral order from result array function printSpiral(res) { console.log(res.join(" ")); }
let root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(7); root.left.right = new Node(6); root.right.left = new Node(5); root.right.right = new Node(4);
// First get result let res = findSpiral(root);
// Then pass it to printSpiral printSpiral(res);
`
Using Deque - O(n) Time and O(n) Space
The idea is to use Doubly Ended Queues, then push and pop the nodes from each end in alternate order.
**Steps to solve the problem:
- Start with a deque containing the root and a flag variable
reverse = true. - While deque is not empty:
- Process all nodes of the current level.
- If
reverse→ pop from back, add children (right → left) at front. - Else → pop from front, add children (left → right) at back.
- Flip
reversefor each level. C++ `
#include #include #include using namespace std;
struct Node { int data; Node* left; Node* right;
Node(int x) {
data = x;
left = nullptr;
right = nullptr;
}};
vector findSpiral(Node* root) { vector res; if (!root) return res;
deque<Node*> dq;
dq.push_back(root);
bool reverse = true;
while (!dq.empty()) {
int n = dq.size();
while (n--) {
// Push right first if reverse is true
if (reverse) {
Node* curr = dq.back();
dq.pop_back();
res.push_back(curr->data);
if (curr->right) dq.push_front(curr->right);
if (curr->left) dq.push_front(curr->left);
}
// Else push left first
else {
Node* curr = dq.front();
dq.pop_front();
res.push_back(curr->data);
if (curr->left) dq.push_back(curr->left);
if (curr->right) dq.push_back(curr->right);
}
}
reverse = !reverse;
}
return res;}
// New function to print spiral order from result vector void printSpiral(vector& res) { for (int x : res) cout << x << " "; cout << endl; }
int main() { Node* root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(7); root->left->right = new Node(6); root->right->left = new Node(5); root->right->right = new Node(4);
// First get result
vector<int> res = findSpiral(root);
// Then pass it to printSpiral
printSpiral(res);
return 0;}
Java
import java.util.*;
class Node { int data; Node left, right;
Node(int x) {
data = x;
left = right = null;
}}
public class SpiralTraversal {
static List<Integer> findSpiral(Node root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
Deque<Node> dq = new LinkedList<>();
dq.addLast(root);
boolean reverse = true;
while (!dq.isEmpty()) {
int n = dq.size();
while (n-- > 0) {
// Push right first if reverse is true
if (reverse) {
Node curr = dq.removeLast();
res.add(curr.data);
if (curr.right != null) dq.addFirst(curr.right);
if (curr.left != null) dq.addFirst(curr.left);
}
// Else push left first
else {
Node curr = dq.removeFirst();
res.add(curr.data);
if (curr.left != null) dq.addLast(curr.left);
if (curr.right != null) dq.addLast(curr.right);
}
}
reverse = !reverse;
}
return res;
}
// New function to print spiral order from result list
static void printSpiral(List<Integer> res) {
for (int x : res)
System.out.print(x + " ");
System.out.println();
}
public static void main(String[] args) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(7);
root.left.right = new Node(6);
root.right.left = new Node(5);
root.right.right = new Node(4);
List<Integer> res = findSpiral(root);
printSpiral(res);
}}
Python
from collections import deque
class Node: def init(self, x): self.data = x self.left = None self.right = None
def findSpiral(root): res = [] if not root: return res
dq = deque()
dq.append(root)
reverse = True
while dq:
n = len(dq)
while n > 0:
# Push right first if reverse is true
if reverse:
curr = dq.pop()
res.append(curr.data)
if curr.right:
dq.appendleft(curr.right)
if curr.left:
dq.appendleft(curr.left)
# Else push left first
else:
curr = dq.popleft()
res.append(curr.data)
if curr.left:
dq.append(curr.left)
if curr.right:
dq.append(curr.right)
n -= 1
reverse = not reverse
return resNew function to print spiral order from result list
def printSpiral(res): print(" ".join(map(str, res)))
if name == "main": root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(7) root.left.right = Node(6) root.right.left = Node(5) root.right.right = Node(4)
res = findSpiral(root)
printSpiral(res)C#
using System; using System.Collections.Generic;
class Node { public int data; public Node left, right;
public Node(int x) {
data = x;
left = right = null;
}}
class SpiralTraversal {
static List<int> findSpiral(Node root) {
List<int> res = new List<int>();
if (root == null) return res;
LinkedList<Node> dq = new LinkedList<Node>();
dq.AddLast(root);
bool reverse = true;
while (dq.Count > 0) {
int n = dq.Count;
while (n-- > 0) {
// Push right first if reverse is true
if (reverse) {
Node curr = dq.Last.Value;
dq.RemoveLast();
res.Add(curr.data);
if (curr.right != null) dq.AddFirst(curr.right);
if (curr.left != null) dq.AddFirst(curr.left);
}
// Else push left first
else {
Node curr = dq.First.Value;
dq.RemoveFirst();
res.Add(curr.data);
if (curr.left != null) dq.AddLast(curr.left);
if (curr.right != null) dq.AddLast(curr.right);
}
}
reverse = !reverse;
}
return res;
}
// New function to print spiral order from result list
static void printSpiral(List<int> res) {
foreach (int x in res)
Console.Write(x + " ");
Console.WriteLine();
}
static void Main() {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(7);
root.left.right = new Node(6);
root.right.left = new Node(5);
root.right.right = new Node(4);
List<int> res = findSpiral(root);
printSpiral(res);
}}
JavaScript
class Node { constructor(x) { this.data = x; this.left = null; this.right = null; } }
function findSpiral(root) { let res = []; if (!root) return res;
let dq = [];
dq.push(root);
let reverse = true;
while (dq.length > 0) {
let n = dq.length;
while (n-- > 0) {
// Push right first if reverse is true
if (reverse) {
let curr = dq.pop();
res.push(curr.data);
if (curr.right) dq.unshift(curr.right);
if (curr.left) dq.unshift(curr.left);
}
// Else push left first
else {
let curr = dq.shift();
res.push(curr.data);
if (curr.left) dq.push(curr.left);
if (curr.right) dq.push(curr.right);
}
}
reverse = !reverse;
}
return res;}
// New function to print spiral order from result array function printSpiral(res) { console.log(res.join(" ")); } // Driver Code let root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(7); root.left.right = new Node(6); root.right.left = new Node(5); root.right.right = new Node(4);
let res = findSpiral(root); printSpiral(res);
`


