zoukankan      html  css  js  c++  java
  • [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 }
  • 相关阅读:
    Linux命令: 向文件写内容,编辑文件,保存文件,查看文件,不保存文件
    SQL: 左连接,右连接,内连接,左外连接,右外连接,完全连接
    Python: 没有switch-case语句
    Python:键盘输入input
    Python: 猴子分桃。海滩上有一堆桃子,五只猴子来分。
    error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
    Scrapy安装
    Linux: 回到根目录cd /
    怎么查看是否安装Scrapy
    虚拟环境Scrapy安装
  • 原文地址:https://www.cnblogs.com/lz87/p/7349794.html
Copyright © 2011-2022 走看看