[GeeksForGeeks] Level order traversal in spiral form of a binary tree.

Write a function to print spiral order traversal of a binary tree. For below tree, function should print 1, 2, 3, 4, 5, 6, 7.

  


Solution.

For a normal level order traversal of a binary tree, we traverse every level from left to right. To achieve this, a queue

is used to store the next level‘s nodes.

To do this traversal in a spiral order, we need to be able to add and poll nodes in a queue from its front and back.

This implies using Deque and a boolean flag to indicate if we are traversing from left to right or from right to left.

When traversing from right to left, we poll nodes from the end of dq; First add its right child node, then its left child node to the front of dq.

When traversing from left to right, we poll nodes from the front of dq; First add its left, then right child node to the back of dq.

After adding all nodes in one level, reverse the traversal direction flag.

 1 import java.util.ArrayList;
 2 import java.util.LinkedList;
 3 import java.util.Deque;
 4
 5 class LOTSpiralNode {
 6     int val;
 7     LOTSpiralNode left, right;
 8     LOTSpiralNode(int v) {
 9         this.val = v;
10         this.left = null;
11         this.right = null;
12     }
13     LOTSpiralNode(LOTSpiralNode t){
14         this.val = t.val;
15         this.left = t.left;
16         this.right = t.right;
17     }
18 }
19 public class LOTSpiral {
20     public ArrayList<Integer> levelOrderTraversalSpiral(LOTSpiralNode root) {
21         ArrayList<Integer> list = new ArrayList<Integer>();
22         if(root == null) {
23             return list;
24         }
25         Deque<LOTSpiralNode> dq = new LinkedList<LOTSpiralNode>();
26         boolean leftToRight = false;
27         dq.add(root);
28         while(!dq.isEmpty()) {
29             int size = dq.size();
30             for(int i = 0; i < size; i++) {
31                 LOTSpiralNode curr;
32                 if(leftToRight) {
33                     curr = dq.pollFirst();
34                     if(curr.left != null) {
35                         dq.addLast(curr.left);
36                     }
37                     if(curr.right != null) {
38                         dq.addLast(curr.right);
39                     }
40                 }
41                 else{
42                     curr = dq.pollLast();
43                     if(curr.right != null) {
44                         dq.addFirst(curr.right);
45                     }
46                     if(curr.left != null) {
47                         dq.addFirst(curr.left);
48                     }
49                 }
50                 list.add(curr.val);
51             }
52             leftToRight = !leftToRight;
53         }
54         return list;
55     }
56 }
时间: 2024-10-20 15:01:57

[GeeksForGeeks] Level order traversal in spiral form of a binary tree.的相关文章

LeetCode: Binary Tree Level Order Traversal 层序遍历二叉树

Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order trav

Binary Tree Level Order Traversal 、Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9  20 /  \ 15   7 return its level o

Java [Leetcode 107]Binary Tree Level Order Traversal II

题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order

[LeetCode]Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 re

Binary Tree Zigzag Level Order Traversal

原题: 题目解析:这个问题的实质是要我们按成访问二叉树的结点,并返回每层访问的结果,这里要求走Z字,其实就是一行正向一行反向. /* the kernel idea is visit a binary search tree in level and the additional work we have to label the end of one level. */ vector<vector<int> > zigzagLevelOrder(TreeNode *root) {

【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】

[107-Binary Tree Level Order Traversal II(二叉树层序遍历II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example

【Lintcode】069.Binary Tree Level Order Traversal

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). Example Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 题解: 三种

103. Binary Tree Zigzag Level Order Traversal (Tree, Queue; BFS)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree [3,9,20,null,null,15,7], 3   / \  9  20    /  \  

102. Binary Tree Level Order Traversal (Tree, Queue; BFS)

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree [3,9,20,null,null,15,7], 3   / \  9  20    /  \   15   7 return its level order traversal as: [  [3],