18. Level Order in spiral form
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given a binary tree, return its spiral (zigzag) level-order traversal. In spiral order traversal:
At even-indexed levels (0-based), traverse nodes from right to left.
At odd-indexed levels, traverse nodes from left to right.
Return a list containing all node values in spiral order.
๐ Examples
Example 1:
Input:
root = [1, 3, 2]
Output:
[1, 3, 2]
Explanation:
Level 0 (even) โ right to left: 1
Level 1 (odd) โ left to right: 3 2
Example 2:
Input:
root = [10, 20, 30, 40, 60]
Output:
[10, 20, 30, 60, 40]
Example 3:
Input:
root = [1, 2, N, 4]
Output:
[1, 2, 4]
๐ Constraints
1 โค number of nodes โค 10โต
0 โค node->data โค 10โต
โ
My Approach
Using Double-Ended Queue (Deque)
We use a deque
to simulate two-directional traversal:
For even-indexed levels, we traverse from right to left using
pop_back()
and push children to the front.For odd-indexed levels, we traverse from left to right using
pop_front()
and push children to the back.
We alternate the direction at each level using a boolean flag.
Algorithm Steps:
Initialize deque
q
and push root node.While
q
is not empty:Traverse the current level based on direction:
If flag is
True
(even), pop from back and push rightโleft children to front.If flag is
False
(odd), pop from front and push leftโright children to back.
Flip the flag at the end of each level.
๐งฎ Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), as we visit each node exactly once in level order.
Expected Auxiliary Space Complexity: O(n), due to storing up to an entire level of nodes in the deque.
๐ง Code (C++)
class Solution {
public:
vector<int> findSpiral(Node* r) {
if (!r) return {};
vector<int> a;
deque<Node*> q{r};
bool f = 1;
while (!q.empty()) {
int n = q.size();
while (n--) {
Node* x = f ? q.back() : q.front();
f ? q.pop_back() : q.pop_front();
a.push_back(x->data);
if (f) {
if (x->right) q.push_front(x->right);
if (x->left) q.push_front(x->left);
} else {
if (x->left) q.push_back(x->left);
if (x->right) q.push_back(x->right);
}
}
f = !f;
}
return a;
}
};
๐งโ๐ป Code (Java)
class Solution {
public ArrayList<Integer> findSpiral(Node r) {
if (r == null) return new ArrayList<>();
ArrayList<Integer> a = new ArrayList<>();
Deque<Node> q = new ArrayDeque<>();
q.add(r);
boolean f = true;
while (!q.isEmpty()) {
int n = q.size();
while (n-- > 0) {
Node x = f ? q.pollLast() : q.pollFirst();
a.add(x.data);
if (f) {
if (x.right != null) q.addFirst(x.right);
if (x.left != null) q.addFirst(x.left);
} else {
if (x.left != null) q.addLast(x.left);
if (x.right != null) q.addLast(x.right);
}
}
f = !f;
}
return a;
}
}
๐ Code (Python)
class Solution:
def findSpiral(self, r):
if not r: return []
a, q, f = [], deque([r]), True
while q:
for _ in range(len(q)):
x = q.pop() if f else q.popleft()
a.append(x.data)
if f:
if x.right: q.appendleft(x.right)
if x.left: q.appendleft(x.left)
else:
if x.left: q.append(x.left)
if x.right: q.append(x.right)
f = not f
return a
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Letโs make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated