ExamAdda Logo

Binary Tree Zigzag Level Order Traversal

Easy

Given the root of a binary tree, return its zigzag level order traversal.

The traversal should visit:

  • First level: left → right
  • Second level: right → left
  • Third level: left → right
  • Continue alternating for every level.

For example:

   3      

 / \      

9   20       

     /  \     

   15   7

 

Zigzag traversal:

[  [3],  [20, 9],  [15, 7] ]

 

This is essentially BFS with alternating direction at each level.

Example 1

Input

root = [3,9,20,null,null,15,7]

Output

[[3],[20,9],[15,7]]

Explanation

The first level is traversed left to right:

[3]

 

The second level is traversed right to left:

[20,9]

 

The third level is traversed left to right:

[15,7]

 

Example 2

Input

root = [1]

Output

[[1]]

Explanation

There is only one level, so no direction change is required.

Constraints

0 <= number of nodes <= 2000
-100 <= Node.val <= 100

Hints:

Hint 1

Use BFS level-order traversal with a queue.

Hint 2

Process one complete level at a time.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 12, 2026

Last updated

Aug 12, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output