首页 > 其他分享 >二叉树:自下而上,从左到右的层次遍历

二叉树:自下而上,从左到右的层次遍历

时间:2023-08-15 10:27:45浏览次数:38  
标签:遍历 TreeNode true top 从左到右 二叉树 return false data

利用栈先进后出的特性,将出队元素依次进栈,然后依次出栈即可完成。

#include <stdio.h>
#include <stdlib.h>

#define MaxSize 100

//二叉树的结点类型 
typedef struct Node{
    int data;
    struct Node *lchild,*rchild;
}TreeNode,*Tree;

//队列的结点类型 
typedef struct{
    TreeNode* data[MaxSize];
    int front,rear;
}Queue;

//栈的结点类型 
typedef struct{
    TreeNode* data[MaxSize];
    int top;
}Stack; 

//初始化队列 
void InitQueue(Queue &Q)
{
    Q.front=Q.rear=0;
}

//队列判空 
bool isEmpty(Queue Q)
{
    if(Q.front==Q.rear)
        return true;
    return false;
}

//队列判满 
bool isFull(Queue Q)
{
    if((Q.rear+1)%MaxSize==Q.front)
        return true;
    return false;
}

//入队 
bool EnQueue(Queue &Q,TreeNode *p)
{
    if(isFull(Q))
        return false;
    Q.data[Q.rear]=p;
    Q.rear=(Q.rear+1)%MaxSize;
    return true;
}

//出队 
bool DeQueue(Queue &Q,TreeNode* &p)
{
    if(isEmpty(Q))
        return false;
    p=Q.data[Q.front];
    Q.front=(Q.front+1)%MaxSize;
    return true;
}

//初始化栈 
void InitStack(Stack &S)
{
    S.top=-1;
}

//栈判空 bool StackIsEmpty(Stack S) { if(S.top==-1) return true; return false; } //进栈 bool Push(Stack &S,TreeNode *p) { if(S.top==MaxSize-1) return false; S.data[++S.top]=p; return true; } //出栈 bool Pop(Stack &S,TreeNode* &p) { if(S.top==-1) return false; p=S.data[S.top--]; return true; } //获取栈顶元素 bool GetTop(Stack S,TreeNode* &p) { if(S.top==-1) return false; p=S.data[S.top]; return true; } //先序创建二叉树 void CreateTree(Tree &T) { int x; scanf("%d",&x); if(x==-1) { T=NULL; return; } else { T=(Tree)malloc(sizeof(TreeNode)); T->data=x; printf("输入%d的左结点:",x); CreateTree(T->lchild); printf("输入%d的右结点:",x); CreateTree(T->rchild); } } //自下而上,从右到左的层次遍历 void LevelOrder(Tree T) { Stack S; InitStack(S); Queue Q; InitQueue(Q); TreeNode *p=T; EnQueue(Q,p); while(!isEmpty(Q)) { DeQueue(Q,p); Push(S,p); if(p->lchild!=NULL) EnQueue(Q,p->lchild); if(p->rchild!=NULL) EnQueue(Q,p->rchild); } while(!StackIsEmpty(S)) { Pop(S,p); printf("%d ",p->data); } } int main() { Tree T; CreateTree(T); LevelOrder(T); return 0; }

 

标签:遍历,TreeNode,true,top,从左到右,二叉树,return,false,data
From: https://www.cnblogs.com/simpleset/p/17630589.html

相关文章

  • 【剑指Offer】 24、二叉树中和为某一值的路径
    【剑指Offer】24、二叉树中和为某一值的路径题目描述:输入一颗二叉树的根结点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意:在返回值的list中,数组长度大的数组靠前)解题思路:本题实......
  • 【剑指Offer】23、二叉搜索树的后序遍历序列
    【剑指Offer】23、二叉搜索树的后序遍历序列题目描述:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。解题思路:对于后续遍历序列,序列的最后一个值一定是树的根结点,而由二叉搜索树的性质......
  • [代码随想录]Day17-二叉树part06
    题目:654.最大二叉树思路:和前中序构造树差不多的方法,以前是返回值,现在是返回树代码:/***Definitionforabinarytreenode.*typeTreeNodestruct{*Valint*Left*TreeNode*Right*TreeNode*}*/funcconstructMaximumBinaryTree(nums[]in......
  • 二叉树
    1概念二叉树BinaryTree是n个结点的有限集合。它或者是空集n=0,或者是由一个根结点以及两颗互不相交、分别称为左子树和右子树的二叉树组成。   76 354 21根结点根结点的左子树根结点的右子树二叉树与普通有序树不同,二叉树严格区分左子和右子,即使只有一个子结点也要区分左......
  • [YsOI2023] 广度优先遍历 逆向输出路径(分层建树拓扑序. LCA)
    今天的模板测试是无向图上的广度优先遍历,【数据删除】马上写好了代码:1#include<cstdio>2#include<cstring>3#include<iostream>4#include<algorithm>5#include<vector>6#include<queue>7usingnamespacestd;8constintmaxn=100005;......
  • element表单验证在遍历中的使用
    之前使用表单验证时,都是对象形式,比较简单,直接按官网demo来即可满足要求,官网链接如下: https://element-plus.gitee.io/zh-CN/component/form.html代码如下:<template><div><el-formref="ruleForm":model="myForm":rules="rules"><el-form......
  • 二叉树的迭代遍历-栈
    二叉树的迭代遍历-栈二叉树的递归遍历书写简单,做题时能够帮助快速完成dfs但是往往有某些面试官或者题目要求,使用迭代法完成树的遍历作为复习材料,不导出推导过程,只给出核心记忆点TreeNodepublicclassTreeNode{intval;TreeNodeleft;TreeNoderight;......
  • 二叉树的层次遍历
    #include<stdio.h>#include<stdlib.h>#defineMaxSize100typedefstructTreeNode{ intdata; structTreeNode*lchild,*rchild;}TreeNode,*Tree;typedefstruct{ TreeNode*data[MaxSize]; intfront; intrear;}Queue;voidInitQueue(Queue......
  • 二叉树的非递归遍历
    //非递归操作#include<stdio.h>#include<stdlib.h>#defineMaxSize200typedefstructTreeNode{intdata;structTreeNode*lchild,*rchild;}TreeNode,*Tree;typedefstruct{Treedata[MaxSize];inttop;}Stack;voidInitStack(S......
  • 二叉树的递归遍历
    #include<stdio.h>#include<stdlib.h>typedefstructTNode{intdata;structTNode*lchild,*rchild;}TreeNode,*Tree;/*在这段代码中,递归函数`CreateTree`在执行`return`语句后,会立即返回到调用它的上一层递归调用。但是,整个递归过程并没有结束,仍然会......