给定一棵二叉树,以这样的方式打印级别顺序遍历:从左至右打印前两个级别,从右至左打印下两个级别,然后从左至右打印下两个级别,依此类推。因此,问题是每两个级别后,二叉树的级别顺序遍历的方向就会反转。
例子:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \ / \ / \
8 9 3 1 4 2 7 2
/ / \ \
16 17 18 19
Output:
1
2 3
7 6 5 4
2 7 2 4 1 3 9 8
16 17 18 19
In the above example, the first two levels
are printed from left to right, next two
levels are printed from right to left,
and then the last level is printed from
left to right.
方法:在上一篇文章中,已经完成了使用队列和堆栈进行级别顺序遍历以打印元素的操作。在这里使用了递归方法来打印每个级别中的元素。遍历树中的每个级别,对于每个级别,请检查方向。使用标志来了解树中遍历的方向。如果该标志设置为true ,则在特定级别中从右到左打印节点。如果该标志设置为false ,则从左到右打印该级别的节点。最初,将标志设置为False,每2个级别后,标志将其值更改为true,反之亦然。
下面是上述方法的实现。
C++
// C++ program level order traversal
// with direction change
// after every two levels
#include
using namespace std;
struct node {
int data;
node *left, *right;
} * temp;
// inserts new node
node* newNode(int data)
{
temp = new node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// function to print current level
void printCurrLevel(node* root, int level, bool flag)
{
if (!root)
return;
if (level == 1) {
cout << root->data << " ";
return;
}
else {
// If the flag is true, we have to print
// level from RIGHT to LEFT.
if (flag) {
printCurrLevel(root->right, level - 1, flag);
printCurrLevel(root->left, level - 1, flag);
}
// If the flag is false, we have to print
// level from LEFT to RIGHT.
else {
printCurrLevel(root->left, level - 1, flag);
printCurrLevel(root->right, level - 1, flag);
}
}
}
// This function returns the height of tree.
int height(node* root)
{
if (!root)
return 0;
// left subtree
int lh = height(root->left);
// right subtree
int rh = height(root->right);
return 1 + max(lh, rh);
}
// Function to traverse level-wise and
// print nodes
void modifiedLevelOrder(node* root)
{
int h = height(root);
// Variable to choose direction.
bool flag = false;
for (int i = 1; i <= h; i++) {
printCurrLevel(root, i, flag);
cout << endl;
// change direction after every two levels.
if (i % 2 == 0)
flag = !flag;
}
}
// Driver Code
int main()
{
// create tree that is given
// in the example
node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->left->left->left = newNode(8);
root->left->left->right = newNode(9);
root->left->right->left = newNode(3);
root->left->right->right = newNode(1);
root->right->left->left = newNode(4);
root->right->left->right = newNode(2);
root->right->right->left = newNode(7);
root->right->right->right = newNode(2);
root->left->right->left->left = newNode(16);
root->left->right->left->right = newNode(17);
root->right->left->right->left = newNode(18);
root->right->right->left->right = newNode(19);
modifiedLevelOrder(root);
return 0;
}
Java
// Java implementation of above idea
import java.util.*;
class GFG
{
static class node
{
int data;
node left, right;
}
static node temp;
// inserts new node
static node newNode(int data)
{
temp = new node();
temp.data = data;
temp.left = temp.right = null;
return temp;
}
// function to print current level
static void printCurrLevel(node root, int level, boolean flag)
{
if (root == null)
return;
if (level == 1)
{
System.out.print(root.data + " ");
return;
}
else
{
// If the flag is true, we have to print
// level from RIGHT to LEFT.
if (flag)
{
printCurrLevel(root.right, level - 1, flag);
printCurrLevel(root.left, level - 1, flag);
}
// If the flag is false, we have to print
// level from LEFT to RIGHT.
else
{
printCurrLevel(root.left, level - 1, flag);
printCurrLevel(root.right, level - 1, flag);
}
}
}
// This function returns the height of tree.
static int height(node root)
{
if (root == null)
return 0;
// left subtree
int lh = height(root.left);
// right subtree
int rh = height(root.right);
return 1 + Math.max(lh, rh);
}
// Function to traverse level-wise and
// print nodes
static void modifiedLevelOrder(node root)
{
int h = height(root);
// Variable to choose direction.
boolean flag = false;
for (int i = 1; i <= h; i++)
{
printCurrLevel(root, i, flag);
System.out.println("");
// change direction after every two levels.
if (i % 2 == 0)
flag = !flag;
}
}
// Driver Code
public static void main(String[] args)
{
// create tree that is given
// in the example
node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.left.left.left = newNode(8);
root.left.left.right = newNode(9);
root.left.right.left = newNode(3);
root.left.right.right = newNode(1);
root.right.left.left = newNode(4);
root.right.left.right = newNode(2);
root.right.right.left = newNode(7);
root.right.right.right = newNode(2);
root.left.right.left.left = newNode(16);
root.left.right.left.right = newNode(17);
root.right.left.right.left = newNode(18);
root.right.right.left.right = newNode(19);
modifiedLevelOrder(root);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program level order traversal with
# direction change after every two levels
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# function to print current level
def printCurrLevel(root, level, flag):
if root == None:
return
if level == 1:
print(root.data, end = " ")
return
else:
# If the flag is true, we have to
# print level from RIGHT to LEFT.
if flag:
printCurrLevel(root.right,
level - 1, flag)
printCurrLevel(root.left,
level - 1, flag)
# If the flag is false, we have to
# print level from LEFT to RIGHT.
else:
printCurrLevel(root.left,
level - 1, flag)
printCurrLevel(root.right,
level - 1, flag)
# This function returns the
# height of tree.
def height(root):
if root == None:
return 0
# left subtree
lh = height(root.left)
# right subtree
rh = height(root.right)
return 1 + max(lh, rh)
# Function to traverse level-wise
# and print nodes
def modifiedLevelOrder(root):
h = height(root)
# Variable to choose direction.
flag = False
for i in range(1, h + 1):
printCurrLevel(root, i, flag)
print()
# change direction after every
# two levels.
if i % 2 == 0:
flag = not flag
# Driver Code
if __name__ == "__main__":
# create tree that is given
# in the example
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.left = Node(6)
root.right.right = Node(7)
root.left.left.left = Node(8)
root.left.left.right = Node(9)
root.left.right.left = Node(3)
root.left.right.right = Node(1)
root.right.left.left = Node(4)
root.right.left.right = Node(2)
root.right.right.left = Node(7)
root.right.right.right = Node(2)
root.left.right.left.left = Node(16)
root.left.right.left.right = Node(17)
root.right.left.right.left = Node(18)
root.right.right.left.right = Node(19)
modifiedLevelOrder(root)
# This code is contributed by Rituraj Jain
C#
// C# implementation of above idea
using System;
class GFG
{
public class node
{
public int data;
public node left, right;
}
static node temp;
// inserts new node
static node newNode(int data)
{
temp = new node();
temp.data = data;
temp.left = temp.right = null;
return temp;
}
// function to print current level
static void printCurrLevel(node root, int level, Boolean flag)
{
if (root == null)
return;
if (level == 1)
{
Console.Write(root.data + " ");
return;
}
else
{
// If the flag is true, we have to print
// level from RIGHT to LEFT.
if (flag)
{
printCurrLevel(root.right, level - 1, flag);
printCurrLevel(root.left, level - 1, flag);
}
// If the flag is false, we have to print
// level from LEFT to RIGHT.
else
{
printCurrLevel(root.left, level - 1, flag);
printCurrLevel(root.right, level - 1, flag);
}
}
}
// This function returns the height of tree.
static int height(node root)
{
if (root == null)
return 0;
// left subtree
int lh = height(root.left);
// right subtree
int rh = height(root.right);
return 1 + Math.Max(lh, rh);
}
// Function to traverse level-wise and
// print nodes
static void modifiedLevelOrder(node root)
{
int h = height(root);
// Variable to choose direction.
Boolean flag = false;
for (int i = 1; i <= h; i++)
{
printCurrLevel(root, i, flag);
Console.WriteLine("");
// change direction after every two levels.
if (i % 2 == 0)
flag = !flag;
}
}
// Driver Code
public static void Main(String[] args)
{
// create tree that is given
// in the example
node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.left.left.left = newNode(8);
root.left.left.right = newNode(9);
root.left.right.left = newNode(3);
root.left.right.right = newNode(1);
root.right.left.left = newNode(4);
root.right.left.right = newNode(2);
root.right.right.left = newNode(7);
root.right.right.right = newNode(2);
root.left.right.left.left = newNode(16);
root.left.right.left.right = newNode(17);
root.right.left.right.left = newNode(18);
root.right.right.left.right = newNode(19);
modifiedLevelOrder(root);
}
}
/* This code is contributed by PrinciRaj1992 */
输出:
1
2 3
7 6 5 4
2 7 2 4 1 3 9 8
16 17 18 19