每K层后二叉树的Zig-Zag层序遍历
给定一棵二叉树和一个整数K ,任务是以这样一种方式打印层顺序遍历:前K层从左到右打印,接下来的K层从右到左打印,然后接下来的K层从左对等。
例子:
Input: K = 1
1
/ \
2 3
/ \ /
4 9 8
Output:
1
3 2
4 9 8
Explanation: In the above example, first level is printed from left to right
and the second level is printed from right to left, and then last level is
printed from left to right.
Input: K = 3
1
/ \
2 3
/ \ / \
4 5 6 7
/ \ / \ /
8 9 10 11 12
/ \ /
13 14 15
Output:
1
2 3
4 5 6 7
12 11 10 9 8
15 14 13
Explanation: In the above example, first 3 levels are printed from left to right
and the last 2 levels are printed from right to left.
方法:该问题的解决方案基于以下思路:
Start performing level order traversal on the tree from the left most end. After every K level, change the direction of printing the elements.
For this use stack. When the levels are to be printed from the right keep those values in stack and print the stack elements one by one from top. Because of the last in first out property of stack the elements would printed in reverse order.
按照下面提到的步骤来实现上述想法:
- 使用队列执行级别顺序遍历。
- 在每个级别:
- 如果要从左边打印,打印它们并将它们的子节点推入队列。
- 如果要从右侧打印此级别,则将元素推入堆栈并在遍历整个级别后打印它们。
- 如果K个级别被覆盖,则从下一个级别更改打印方向。
下面是上述方法的实现。
C++
// C++ code to implement the approach
#include
using namespace std;
// Binary Tree node
struct Node {
int data;
Node *left, *right;
};
// Function that returns a new node
Node* newNode(int data)
{
Node* temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// Function to print the level order of tree
// by reversing the direction of traversal
// after every n levels
void traverse(Node* root, int n)
{
// NULL check
if (!root)
return;
// Queue for level order traversal
queue q;
// Stack for
// reverse level order traversal
stack s;
// For changing the direction
// of traversal
bool right2left = false;
// To count number of levels
int count = 0;
q.push(root);
while (!q.empty()) {
int size = q.size();
count++;
while (size--) {
root = q.front();
q.pop();
// Prints the nodes from
// left to right
if (right2left == false)
cout << root->data << " ";
// Push the nodes into stack
// for printing from right to left
else
s.push(root);
if (root->left)
q.push(root->left);
if (root->right)
q.push(root->right);
}
// If the condition satisfies
// prints the nodes from right to left.
if (right2left == true) {
while (!s.empty()) {
cout << s.top()->data << " ";
s.pop();
}
}
// If the count becomes n
// then reverse the direction
if (count == n) {
right2left = !right2left;
// Upate the count to 0
count = 0;
}
cout << endl;
}
}
int main()
{
// Create a Binary Tree
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(10);
root->right->left->right = newNode(11);
root->right->right->left = newNode(12);
root->left->left->right->left
= newNode(13);
root->left->left->right->right
= newNode(14);
root->right->left->right->left
= newNode(15);
// Specify K to change the direction
// after every K levels.
int K = 3;
traverse(root, K);
return 0;
}
Javascript
1
2 3
4 5 6 7
12 11 10 9 8
15 14 13
时间复杂度: O(N) 其中 N 是节点数
辅助空间: O(N)