📜  使用单个数组对树进行 Zig Zag 级别顺序遍历

📅  最后修改于: 2022-05-13 01:57:00.878000             🧑  作者: Mango

使用单个数组对树进行 Zig Zag 级别顺序遍历

编写一个函数来打印树的螺旋顺序遍历。对于下面的树,函数应该打印 1、2、3、4、5、6、7。

螺旋顺序

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。



我们已经讨论了带有递归和多个堆栈的 Level Order 中的朴素方法和两种基于堆栈的方法
这种方法背后的想法是首先我们必须采用一个队列、一个方向标志和一个为 NULL 的分离标志

  1. 将根元素插入队列并再次将 NULL 插入队列。
  2. 对于队列中的每个元素,插入其子节点。
  3. 如果遇到 NULL,则检查遍历特定级别的方向是从左到右还是从右到左。如果它是偶数级别,则从左到右遍历,否则按从右到级别的顺序遍历树,即从前面到前一个前面,即从当前 NULL 到已访问过的最后一个 NULL。这一直持续到最后一层,然后循环中断,我们通过检查打印方向来打印剩下的(尚未打印的)内容。

下面是解释的实现

C++
// C++ program to print level order traversal
// in spiral form using a single dequeue
#include 
 
struct Node {
    int data;
    struct Node *left, *right;
};
 
// A utility function to create a new node
struct Node* newNode(int data)
{
    struct Node* node = new struct Node;
    node->data = data;
    node->left = node->right = NULL;
    return (node);
}
 
// function to print the level order traversal
void levelOrder(struct Node* root, int n)
{
    // We can just take the size as H+N which
    // implies the height of the tree with the
    // size of the tree
    struct Node* queue[2 * n];
    int top = -1;
    int front = 1;
    queue[++top] = NULL;
    queue[++top] = root;
    queue[++top] = NULL;
 
    // struct Node* t=root;
    int prevFront = 0, count = 1;
    while (1) {
 
        struct Node* curr = queue[front];
 
        // A level separator found
        if (curr == NULL) {
 
            // If this is the only item in dequeue
            if (front == top)
                break;
 
            // Else print contents of previous level
            // according to count
            else {
                if (count % 2 == 0) {
                    for (int i = prevFront + 1; i < front; i++)
                        printf("%d ", queue[i]->data);
                }
                else {
                    for (int i = front - 1; i > prevFront; i--)
                        printf("%d ", queue[i]->data);
                }
 
                prevFront = front;
                count++;
                front++;
 
                // Insert a new level separator
                queue[++top] = NULL;
 
                continue;
            }
        }
 
        if (curr->left != NULL)
            queue[++top] = curr->left;
        if (curr->right != NULL)
            queue[++top] = curr->right;
        front++;
    }
 
    if (count % 2 == 0) {
        for (int i = prevFront + 1; i < top; i++)
            printf("%d ", queue[i]->data);
    }
    else {
        for (int i = top - 1; i > prevFront; i--)
            printf("%d ", queue[i]->data);
    }
}
 
// Driver code
int main()
{
    struct Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(7);
    root->left->right = newNode(6);
    root->right->left = newNode(5);
    root->right->right = newNode(4);
    levelOrder(root, 7);
 
    return 0;
}


Java
// Java program to print level order traversal
// in spiral form using a single dequeue
class Solution
{
     
static class Node
{
    int data;
    Node left, right;
};
 
// A utility function to create a new node
static Node newNode(int data)
{
    Node node = new Node();
    node.data = data;
    node.left = node.right = null;
    return (node);
}
 
// function to print the level order traversal
static void levelOrder( Node root, int n)
{
    // We can just take the size as H+N which
    // implies the height of the tree with the
    // size of the tree
    Node queue[] = new Node[2 * n];
     
    for(int i = 0; i < 2 * n; i++)
        queue[i] = new Node();
     
    int top = -1;
    int front = 1;
    queue[++top] = null;
    queue[++top] = root;
    queue[++top] = null;
 
    // Node t=root;
    int prevFront = 0, count = 1;
    while (true)
    {
 
        Node curr = queue[front];
 
        // A level separator found
        if (curr == null)
        {
 
            // If this is the only item in dequeue
            if (front == top)
                break;
 
            // Else print contents of previous level
            // according to count
            else
            {
                if (count % 2 == 0)
                {
                    for (int i = prevFront + 1; i < front; i++)
                        System.out.printf("%d ", queue[i].data);
                }
                else
                {
                    for (int i = front - 1; i > prevFront; i--)
                        System.out.printf("%d ", queue[i].data);
                }
 
                prevFront = front;
                count++;
                front++;
 
                // Insert a new level separator
                queue[++top] = null;
 
                continue;
            }
        }
 
        if (curr.left != null)
            queue[++top] = curr.left;
        if (curr.right != null)
            queue[++top] = curr.right;
        front++;
    }
 
    if (count % 2 == 0)
    {
        for (int i = prevFront + 1; i < top; i++)
            System.out.printf("%d ", queue[i].data);
    }
    else
    {
        for (int i = top - 1; i > prevFront; i--)
            System.out.printf("%d ", queue[i].data);
    }
}
 
// Driver code
public static void main(String args[])
{
    Node root = newNode(1);
    root.left = newNode(2);
    root.right = newNode(3);
    root.left.left = newNode(7);
    root.left.right = newNode(6);
    root.right.left = newNode(5);
    root.right.right = newNode(4);
    levelOrder(root, 7);
}
}
 
// This code is contributed by Arnab Kundu


C#
// C# program to print level order traversal
// in spiral form using a single dequeue
using System;
class GFG
{  
public class Node
{
    public int data;
    public Node left, right;
};
 
// A utility function to create a new node
static Node newNode(int data)
{
    Node node = new Node();
    node.data = data;
    node.left = node.right = null;
    return (node);
}
 
// function to print the level order traversal
static void levelOrder( Node root, int n)
{
    // We can just take the size as H+N which
    // implies the height of the tree with the
    // size of the tree
    Node []queue = new Node[2 * n];
     
    for(int i = 0; i < 2 * n; i++)
        queue[i] = new Node();
     
    int top = -1;
    int front = 1;
    queue[++top] = null;
    queue[++top] = root;
    queue[++top] = null;
 
    // Node t=root;
    int prevFront = 0, count = 1;
    while (true)
    {
 
        Node curr = queue[front];
 
        // A level separator found
        if (curr == null)
        {
 
            // If this is the only item in dequeue
            if (front == top)
                break;
 
            // Else print contents of previous level
            // according to count
            else
            {
                if (count % 2 == 0)
                {
                    for (int i = prevFront + 1;
                             i < front; i++)
                        Console.Write(" " + queue[i].data);
                }
                else
                {
                    for (int i = front - 1;
                             i > prevFront; i--)
                        Console.Write(" " + queue[i].data);
                }
 
                prevFront = front;
                count++;
                front++;
 
                // Insert a new level separator
                queue[++top] = null;
 
                continue;
            }
        }
 
        if (curr.left != null)
            queue[++top] = curr.left;
        if (curr.right != null)
            queue[++top] = curr.right;
        front++;
    }
 
    if (count % 2 == 0)
    {
        for (int i = prevFront + 1; i < top; i++)
            Console.Write(" " + queue[i].data);
    }
    else
    {
        for (int i = top - 1; i > prevFront; i--)
            Console.Write(" " + queue[i].data);
    }
}
 
// Driver code
public static void Main(String []args)
{
    Node root = newNode(1);
    root.left = newNode(2);
    root.right = newNode(3);
    root.left.left = newNode(7);
    root.left.right = newNode(6);
    root.right.left = newNode(5);
    root.right.right = newNode(4);
    levelOrder(root, 7);
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
1 2 3 4 5 6 7

时间复杂度: O(n)
辅助空间: O(2*n) = O(n)