Binary Tree Zigzag Level Order Traversal Leetcode Solution

In this post, we are going to solve the Binary Tree Zigzag Level Order Traversal Leetcode Solution problem of Leetcode. This Leetcode problem is done in many programming languages like C++, Java, and Python.

Binary Tree Zigzag Level Order Traversal Leetcode Solution
Binary Tree Zigzag Level Order Traversal Leetcode Solution

Problem

Given the root of a binary tree, return the zigzag level order traversal of its nodes’ values. (i.e., from left to right, then right to left for the next level and alternate between).

Example 1:

Binary Tree Zigzag Level Order Traversal Leetcode Solution
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[20,9],[15,7]]

Example 2:

Input: root = [1]
Output: [[1]]

Example 3:

Input: root = []
Output: []

Constraints:

  • The number of nodes in the tree is in the range [0, 2000].
  • -100 <= Node.val <= 100

Now, lets see the leetcode solution of Binary Tree Zigzag Level Order Traversal Leetcode Solution.

Binary Tree Zigzag Level Order Traversal Leetcode Solution in Python

class Solution:
  def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
    if not root:
      return []

    ans = []
    q = deque([root])
    isLeftToRight = True

    while q:
      currLevel = []
      for _ in range(len(q)):
        if isLeftToRight:
          node = q.popleft()
          currLevel.append(node.val)
          if node.left:
            q.append(node.left)
          if node.right:
            q.append(node.right)
        else:
          node = q.pop()
          currLevel.append(node.val)
          if node.right:
            q.appendleft(node.right)
          if node.left:
            q.appendleft(node.left)
      ans.append(currLevel)
      isLeftToRight = not isLeftToRight

    return ans

Binary Tree Zigzag Level Order Traversal Leetcode Solution in CPP

class Solution {
 public:
  vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
    if (root == nullptr)
      return {};

    vector<vector<int>> ans;
    deque<TreeNode*> q{{root}};
    bool isLeftToRight = true;

    while (!q.empty()) {
      vector<int> currLevel;
      for (int sz = q.size(); sz > 0; --sz)
        if (isLeftToRight) {
          TreeNode* node = q.front();
          q.pop_front();
          currLevel.push_back(node->val);
          if (node->left)
            q.push_back(node->left);
          if (node->right)
            q.push_back(node->right);
        } else {
          TreeNode* node = q.back();
          q.pop_back();
          currLevel.push_back(node->val);
          if (node->right)
            q.push_front(node->right);
          if (node->left)
            q.push_front(node->left);
        }
      ans.push_back(currLevel);
      isLeftToRight = !isLeftToRight;
    }

    return ans;
  }
};

Binary Tree Zigzag Level Order Traversal Leetcode Solution in Java

class Solution {
  public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
    if (root == null)
      return new ArrayList<>();

    List<List<Integer>> ans = new ArrayList<>();
    Deque<TreeNode> q = new ArrayDeque<>(Arrays.asList(root));
    boolean isLeftToRight = true;

    while (!q.isEmpty()) {
      List<Integer> currLevel = new ArrayList<>();
      for (int sz = q.size(); sz > 0; --sz)
        if (isLeftToRight) {
          TreeNode node = q.pollFirst();
          currLevel.add(node.val);
          if (node.left != null)
            q.addLast(node.left);
          if (node.right != null)
            q.addLast(node.right);
        } else {
          TreeNode node = q.pollLast();
          currLevel.add(node.val);
          if (node.right != null)
            q.addFirst(node.right);
          if (node.left != null)
            q.addFirst(node.left);
        }
      ans.add(currLevel);
      isLeftToRight = !isLeftToRight;
    }

    return ans;
  }
}

Note: This problem Binary Tree Zigzag Level Order Traversal is generated by Leetcode but the solution is provided by Chase2learn This tutorial is only for Educational and Learning purposes.

NEXT: Maximum Depth of Binary Tree Leetcode Solution

Sharing Is Caring