螺旋遍历_螺旋形式的水平阶遍历
螺旋遍歷
Problem statement:
問題陳述:
Write a program to print Level Order Traversal in spiral form of a binary tree.
編寫一個程序以二叉樹的螺旋形式打印Level Level Traversal 。
Example:
例:
For the above tree: Basic level order traversal:27 52 6 95 11 4 Level order traversal in spiral form:27 5 (left to right)9 6 2 (right to left)5 11 4 (again left to right)Solution:
解:
The solution will, of course, surround basic level order traversal. The spiral order means - It will go from left to right for one level, then right to left for next level and again left to right for the next one and so on.
當(dāng)然,解決方案將圍繞基本級別的順序遍歷 。 螺旋順序表示-它將從左向右移動一個級別,然后從右向左移動到下一個級別,再從左向右移動到下一個級別,依此類推。
We need to modify our basic level order traversal.
我們需要修改基本的層級遍歷 。
We can do the flipping of direction (left → right then right → left so on ...) by keeping a flag variable which will be updated at end of each level.
我們可以通過保持標(biāo)記變量來進行方向翻轉(zhuǎn)(左→右然后右→左等等),這將在每個級別的末尾進行更新。
Pre-requisite: Root to tree
先決條件:從樹到根
1. Declare flag as 1(true); 2. Declare a queue q to store pointer to nodes(node*); 3. Declare a stack s which helps us for flipping. 4. Print the root as we are not going to bother about root level; 5. IF(root->left) //left child existsENQUEUE(q, root->left);END IFIF(root->right) //right child existsENQUEUE(q, root->right);END IFIF root has no childRETURN BACK //nothing to print moreELSEq.push(NULL); //to indicate end of 1st level6. //Here goes the modified level order traversalWhen flag=1 its left-to right flag=0 its right to leftwhile (q is not empty){temp=DEQUEUE(q); IF(temp==NULL) //end of last traversed levelIF (q is not empty)ENQUEUE (q, NULL);END IFIF (flag==0)Pop and print data from stack until stack is emptyEND IF flag=1-flag; //flip flag for next level 1 to 0 or 0 to 1ELSEIF(flag == 1)Print temp->data; //left to right printing ELSEPush temp->data to stack s; //this makes right to left //printing as rightmost node will be at the top of stackEND IF-ELSE// basic level order traversal (direction left to right)IF(root->left) //left child existsENQUEUE(q, root->left);END IFIF (root->right) //right child existsENQUEUE(q, root->right);END IFEND IF-ELSE (outer one)END WHILE loop .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}Example with Explanation:
解釋示例:
For the above tree root is being printed first without any constraint2For the first level flag is 1Thus it prints immediately while accessing temp node7 5 (since basic traversal direction is always left to right)At the end of level flag flips to 0So while traversing instead of printing nodes at once, nodes get stored in stackAt the end of level all the nodes data being popped and printed.In stack9(top)62Thus printing9 6 2 (right to left)Flag again flipped to 1Basic left to right printing5 11 4So the output is in spiral orderC++ implementation:
C ++實現(xiàn):
#include <bits/stdc++.h> using namespace std;// tree node is defined class Node{public:int data;Node *left;Node *right; };// creating new node Node* newnode(int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } void printSpiral(Node *root) {Node* temp;int flag=1; queue<Node*> q;stack<int> s;cout<<root->data<<"\n";if(root->left)q.push(root->left);if(root->right)q.push(root->right);if(!root->left && !root->right)return;q.push(NULL);while(!q.empty()){temp=q.front();q.pop();if(temp==NULL){if(!q.empty())q.push(NULL);if(flag==0){while(!s.empty()){cout<<s.top()<<" ";s.pop();}}flag=1-flag;cout<<endl;}else{if(flag){cout<<temp->data<<" ";}else{s.push(temp->data);}if(temp->left)q.push(temp->left);if(temp->right)q.push(temp->right);}} }int main() { //**same tree is builted as shown in example**Node *root=newnode(2); root->left= newnode(7); root->right= newnode(5); root->right->right=newnode(9);root->right->right->left=newnode(4);root->left->left=newnode(2); root->left->right=newnode(6);root->left->right->left=newnode(5);root->left->right->right=newnode(11);cout<<"Level Order traversal in spiral form of ";cout<<"the binary tree is :"<<endl; printSpiral(root); return 0; }Output
輸出量
Level Order traversal in spiral form of the binary tree is : 2 7 5 9 6 2 5 11 4翻譯自: https://www.includehelp.com/icp/level-order-traversal-in-spiral-form.aspx
螺旋遍歷
總結(jié)
以上是生活随笔為你收集整理的螺旋遍历_螺旋形式的水平阶遍历的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 孕妇nt检查多少钱啊?
- 下一篇: 带有Python示例的math.sin(