首页 > 其他分享 >1020 Tree Traversals

1020 Tree Traversals

时间:2023-05-16 09:57:29浏览次数:41  
标签:tmp Node posl 1020 int posr Traversals Tree root

题目:

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
 

Sample Output:

4 1 6 3 5 7 2

 

 

代码:

#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
int n;
int pos[32], in[32];
struct Node{
    int value;
    Node* left;
    Node* right;
};
Node* ConstructTree(int posl, int posr, int inl, int inr){
    if(posl > posr){
        return NULL;
    }
    Node* root = new Node;
    root->value = pos[posr];
    int k;
    for(k = inl; in[k] != pos[posr]; k++);
    int numLeft = k - inl;
    root->left = ConstructTree(posl, posl + numLeft - 1, inl, k - 1);
    root->right = ConstructTree(posl + numLeft, posr - 1, k + 1, inr);
    return root;
}
int main(){
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &pos[i]);
    }
    for(int i = 0; i < n; i++){
        scanf("%d", &in[i]);
    }
    Node* root = ConstructTree(0, n - 1, 0, n - 1);
    bool flag = false;
    queue<Node*> q;
    q.push(root);
    while(!q.empty()){
        Node* tmp = q.front();
        q.pop();
        if(flag == true){
            printf(" ");
        }else{
            flag = true;
        }
        printf("%d", tmp->value);
        if(tmp->left != NULL){
            q.push(tmp->left);
        }
        if(tmp->right != NULL){
            q.push(tmp->right);
        }
    }
    return 0;
}

 

 

总结:

将二叉树以层序遍历输出:

queue<Node*> q;
    q.push(root);
    while(!q.empty()){
        Node* tmp = q.front();
        q.pop();
        if(flag == true){
            printf(" ");
        }else{
            flag = true;
        }
        printf("%d", tmp->value);
        if(tmp->left != NULL){
            q.push(tmp->left);
        }
        if(tmp->right != NULL){
            q.push(tmp->right);
        }
    }

 根据中序和后序遍历构造二叉树:

Node* ConstructTree(int posl, int posr, int inl, int inr){
    if(posl > posr){
        return NULL;
    }
    Node* root = new Node;
    root->value = pos[posr];
    int k;
    for(k = inl; in[k] != pos[posr]; k++);
    int numLeft = k - inl;
    root->left = ConstructTree(posl, posl + numLeft - 1, inl, k - 1);
    root->right = ConstructTree(posl + numLeft, posr - 1, k + 1, inr);
    return root;
}

 

标签:tmp,Node,posl,1020,int,posr,Traversals,Tree,root
From: https://www.cnblogs.com/yccy/p/17403965.html

相关文章

  • CF1777D Score of a Tree 题解
    题目简述给你一个\(n\)个结点根为\(1\)的树。在\(t=0\)时,每个结点都有一个值,为\(0\)或\(1\)。在每一个\(t>0\)时,每个结点的值都会变成其子结点在\(t-1\)时的值的异或和。定义\(S(t)\)为\(t\)时所有结点值的和。定义\(F(A)\)为树在\(0\let\le10^......
  • Java-Day-19( 对集合实现类的选择 + TreeSet + TreeMap )
    Java-Day-19总结-开发中如何选择集合实现类在开发中,选择什么集合实现类,主要取决于业务操作特点,然后根据集合实现类特性进行选择先判断存储的类型(一组对象或一组键值对)一组对象(单列):Collection接口允许重复:List增删多:LinkedList[底层维护了一个双向链......
  • AtCoder Beginner Contest 207 F Tree Patrolling
    洛谷传送门AtCoder传送门简单树形dp。设\(f_{u,i,p=0/1,q=0/1}\)为\(u\)的子树中被覆盖点数为\(i\),\(u\)有没有被覆盖,\(u\)有没有被选。转移树形背包合并即可,需要分类讨论。要注意如果\(u\)没被覆盖,\(v\)选了,或者\(u\)选了,\(v\)没被覆盖,被覆盖点数要\(+1\)。......
  • el-tree 根据多个结果筛选树状图(增加checkbox勾选)
    <template><divclass="wrapper-jjy"><el-dialogtitle="接警员查找"v-model="jjyDialogVisible":draggable="true"width="735px"height="300":close-on-click-modal="true&q......
  • 赫夫曼树HuffmanTree
    赫夫曼树HuffmanTree1.基本概念路径:在树中,从一个节点到另外一个节点之间的分支构成这两个节点之间的路径;路径长度:路径上的分支数称为路径长度;若规定根节点的层数为1,则从根节点到第L层节点的路径长度为L-1;节点的权:对树中的节点赋一个具有某种含义的数值,则该数值称为该......
  • vue node报错ERESOLVE unable to resolve dependency tree
    解决:ERESOLVEunabletoresolvedependencytree小张不厌学于2022-08-2517:00:44发布30549收藏102文章标签:npmvue.js前端版权华为云开发者联盟该内容已被华为云开发者联盟社区收录加入社区NPM版本问题报错的解决方案在安装项目依赖时,很大可能会遇到安装不成功的问题......
  • Layui+dtree实现左边分类列表,右边数据列表
    效果如下代码实现<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>帖子类别</title><metaname="renderer"content="webkit"><metahttp-equiv="X-UA-Compatible&q......
  • el-tree如何拿到所有节点,以及如何控制收起所有节点?
    取所有节点:this.$refs...root.childeNodes收起节点:expanded=false收起全部节点思路将所有节点的expanded设置为false即可。实例html <el-tree ref="myTree" ... ></el-tree>jsfor(letnodeofthis.$refs.myTree.root.childeNodes){ node.expanded=false}......
  • Jdk16中JcTree的使用问题
    因为jdk16进行了强制的模块化使用限制,需要增加add-opens去进行模块的放开,但是如果每次都需要在项目pom文件或者启动命令中增加,非常不优雅。而且很多重复的命令。所以想有没有更好的办法去解决。看了lombok1.18.20中的解决方法,这边来总结一下。lombok这个问题的讨论publica......
  • Win32 SDK TreeView 控件的基本用法,节点的编辑,拖动,添加,删除,弹出菜单
    TreeView控件老是用的稀里糊涂的,写个测试程序总结一下基本用法要注意的是控件是发通知给父窗口处理,因此消息中鼠标的坐标不是相对控件的,要从屏幕坐标转换来。程序是WIN32SDK向导生成的C语言框架。树的操作定义成一个类。 微软网站上TreeView_XXX宏的说明文档好多错误,应该是......