螺旋遍历_螺旋形式的水平阶遍历

螺旋遍历

Problem statement:

问题陈述:

Write a program to print Level Order Traversal in spiral form of a binary tree.

编写一个程序以二叉树的螺旋形式打印Level Level Traversal

Example:

例:

Level order traversal in spiral form
For the above tree:
Basic level order traversal:
2
7 5
2 6 9
5 11 4
Level order traversal in spiral form:
2
7 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.

当然,解决方案将围绕基本级别的顺序遍历 。 螺旋顺序表示-它将从左向右移动一个级别,然后从右向左移动到下一个级别,再从左向右移动到下一个级别,依此类推。

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.

我们可以通过保持标记变量来进行方向翻转(左→右然后右→左等等),这将在每个级别的末尾进行更新。

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 exists
ENQUEUE(q, root->left);
END IF
IF(root->right) //right child exists
ENQUEUE(q, root->right);
END IF
IF root has no child
RETURN BACK //nothing to print more
ELSE
q.push(NULL); //to indicate end of 1st level
6.  //Here goes the modified level order traversal
When flag=1 its left-to right 
flag=0 its right to left
while (q is not empty){
temp=DEQUEUE(q);	
IF(temp==NULL) //end of last traversed level
IF (q is not empty)
ENQUEUE (q, NULL);
END IF
IF (flag==0)
Pop and print data from stack until stack is empty
END IF	
flag=1-flag; //flip flag for next level 1 to 0 or 0 to 1
ELSE
IF(flag == 1)
Print temp->data; //left to right printing		
ELSE
Push temp->data to stack s; //this makes right to left 
//printing as rightmost node will be at the top of stack
END IF-ELSE
// basic level order traversal (direction left to right)
IF(root->left) //left child exists
ENQUEUE(q, root->left);
END IF
IF (root->right) //right child exists
ENQUEUE(q, root->right);
END IF
END IF-ELSE (outer one)
END WHILE loop

Example with Explanation:

解释示例:

    For the above tree root is being printed 
first without any constraint
2
For the first level flag is 1
Thus it prints immediately while accessing temp node
7 5 (since basic traversal direction is always left to right)
At the end of level flag flips to 0
So while traversing instead of printing nodes at once, 
nodes get stored in stack
At the end of level all the nodes data being popped and printed.
In stack
9(top)
6
2
Thus printing
9 6 2 (right to left)
Flag again flipped to 1
Basic left to right printing
5 11 4
So the output is in spiral order

C++ implementation:

C ++实现:

#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

螺旋遍历

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380771.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SharePoint2007安装图文详解二:安装AD(活动目录)及DNS

在上一篇SharePoint2007安装图文详解一&#xff1a;安装IIS及相关组件中已经介绍了IIS及相关组件的安装&#xff0c;本篇将详细介绍AD&#xff08;活动目录&#xff09;的安装。 打开“管理您的服务器”&#xff0c;点击“添加或删除角色” 点击“添加或删除角色”后弹出“配置…

php的foreach

作用&#xff1a;遍历数组 索引数组 形式&#xff1a;foreach(数组 as 值){ //操作 } <?phpheader(content-type:text/html;charsetutf-8);$personarray(DL_one,18,man);foreach($person as $chara){echo $chara,<br>;} ?>2. 关联数组 形式&#xff1a;foreach…

ExtJs实践(3)——xtype名称与控件对应

xtype可作为Ext控件的简写&#xff0c;都会对应一个Ext控件。当然这里你也可以自定义这个xtype&#xff0c;通过自定义的Ext控件来绑定&#xff0c;主要由Ext.reg方法去注册xtype。Ext.all.js里面包含的xtype包含&#xff1a; xtype Class ------------- -------…

Linux 2440 LCD 控制器

嵌入式Linux之我行&#xff0c;主要讲述和总结了本人在学习嵌入式linux中的每个步骤。一为总结经验&#xff0c;二希望能给想入门嵌入式Linux的朋友提供方便。如有错误之处&#xff0c;谢请指正。共享资源&#xff0c;欢迎转载&#xff1a;http://hbhuanggang.cublog.cn 一、开…

php的create_function、function_exists判断函数是否存在

create_function 格式&#xff1a;create_function(‘参数’,‘函数体代码’) 作用&#xff1a;创建匿名函数 <?phpheader(content-type:text/html;charsetutf-8);$func create_function($a,$b, return ($a$b););echo $func(10,20); ?>function_exists 返回值为boo…

php的传值与传址

默认情况下&#xff0c;函数的参数都表示是值传递&#xff0c;但是&#xff0c;如果在定义函数的参数前面加上取地址符&#xff0c;表示传递的是地址值 传值&#xff1a; <?phpheader(content-type:text/html;charsetutf-8);function changeValue($num){$num10;}$value5;c…

php中函数的默认值,参数的顺序

形式参数可以在定义时候给参数赋一个默认值&#xff0c;默认值不能是一个变量&#xff0c;可以是常量&#xff0c;当调用函数时&#xff0c;如果没有参数&#xff0c;函数就会按照默认值处理&#xff0c;如果传递了参数&#xff0c;就按照传递的参数处理 <?phpheader(cont…

10款精选的用于构建良好易用性网站的jQuery插件

这篇随笔收集了10款非常给力的jquery 插件&#xff0c;帮助你构建易用性良好的网站&#xff0c;希望对你有用&#xff01; Embedded help system 看过该插件demo后&#xff0c;感叹道&#xff0c;真是太棒了&#xff01; 点demo里的How to下面的几个链接看看效果:) Embedded …

常见的Java审计代码函数关键字_转载:Java代码审计汇总系列(一)——SQL注入

原文链接&#xff1a;https://cloud.tencent.com/developer/article/1534109一、代码审计相比黑盒渗透的漏洞挖掘方式&#xff0c;代码审计具有更高的可靠性和针对性&#xff0c;更多的是依靠对代码、架构的理解&#xff1b;使用的审计工具一般选择Eclipse或IDEA&#xff1b;审…

php中函数参数个数问题

形参大于实参 <?phpheader(content-type:text/html;charsetutf-8);function fun($name,$sex,$age){echo 名字是.$name,<br>;echo 性别是.$sex,<br>;echo 年龄是.$age,<br>;}fun(DL_one,21); ?>可以看出&#xff0c;能执行但报错 实参个数大于形参个…

php的静态变量static在函数内部

静态变量放在函数内 <?phpheader(content-type:text/html;charsetutf-8);function fun(){static $num1;$num;echo $num,<br>;}fun();fun(); ?>静态变量放在函数内&#xff0c;作用域没变&#xff0c;生命周期变了&#xff0c;页面执行完毕才销毁&#xff0c;静态…

java json帮助类_java 写一个JSON解析的工具类

上面是一个标准的json的响应内容截图&#xff0c;第一个红圈”per_page”是一个json对象&#xff0c;我们可以根据”per_page”来找到对应值是3&#xff0c;而第二个红圈“data”是一个JSON数组&#xff0c;而不是对象&#xff0c;不能直接去拿到里面值&#xff0c;需要遍历数组…

php函数的预加载

php代码的执行过程&#xff1a;词法分析-------语法分析------------编译-----------加载编译的代码--------执行 函数的预加载就是在加载编译的代码过程中&#xff0c;会把函数的代码加载到内存中去&#xff0c;搜易我们在执行代码的时候&#xff0c;函数已经在内存中了 <…

php中的__FUNCTION__

__FUNCTION__:魔术常量&#xff0c;获取函数名 <?phpheader(content-type:text/html;charsetutf-8);function fun(){echo __FUNCTION__;}fun(); ?>

数字图像的大小、所需比特数(二维)

二维数字图像所需的比特数根据公式&#xff1a; 其中&#xff1a; b&#xff1a;数字图像所需的比特数 MN&#xff1a;数字图像的行和列 k&#xff1a;由灰度级算出&#xff0c;公式如下&#xff1a; L&#xff1a;图像的灰度级 比如&#xff1a; 存储一幅大小为 1024x1024&a…

4邻接、8邻接、m邻接

在认识这些之前&#xff0c;我们首先要认识4领域、8领域 4领域&#xff1a; 像素p的坐标是(x,y)&#xff0c;那么他的4领域坐标N4是&#xff1a;&#xff08;x1,y&#xff09;、(x-1,y)&#xff0c;&#xff08;x&#xff0c;y1&#xff09;、(x,y-1) 8领域&#xff1a; 点p的…

java aop注解拦截_Spring AOP 拦截指定注解标识的类或方法

代码DemoAspectComponentOrder(10)public class BidAuthorityProxy {/*** 扫描指定包下的类中使用EnableRoleAuthority注解修饰的类*/Around("within(com.core.annotation.EnableRoleAuthority) && within(com.bid..*)")public Object verifyRoleExecuteComm…

php的文件包含总结 include require include_once require_once

文件包含相当于将另一个文件的代码全部复制到另一个文件中&#xff0c;然后执行。包含文件很有用&#xff0c;如果您需要在网站的多张页面上引用相同的 PHP、HTML 或文本的话。比如说我们在浏览csdn很多页面中&#xff0c;基本都是看到下面的内容&#xff0c;为了不要每次都要写…

php终止脚本执行(exit、die、return)

终止php的脚本执行&#xff0c;我们可以使用exit&#xff0c;die&#xff0c;return 0x01 exit和die&#xff0c; 当程序运行到他们时&#xff0c;直接退出程序&#xff0c;不在运行 <?phpheader(content-type:text/html;charsetutf-8);echo 使用exit前;echo <br>…

php的延时sleep函数

语法&#xff1a;sleep&#xff08;秒数&#xff09; <?phpheader(content-type:text/html;charsetutf-8);sleep&#xff08;5&#xff09;;echo 我的名字是DL_one; ?>输出时可以发现要等待一段时间才能输出 sleep函数在代码测试时很有用