No.1119

题目:由前序后序二叉树序列,推中序,判断是否唯一后输出一组中序序列

思路:前序从前向后找,后序从后向前找,观察正反样例可知,前后序树不唯一在于单一子树是否为左右子树。

判断特征:通过查找后序序列中最后一个结点的前一个在先序中的位置,来确定是否可以划分左右孩子,如果不能,  就将其划分为右孩子(或左孩子),递归建树。

中序遍历输出。

#include <iostream>
using namespace std;
const int maxn = 31; int n, index = 0;
int pre[maxn], post[maxn];
bool flag = true; struct Node {
int data;
Node *lchild, *rchild;
} *root; Node *create(int preL, int preR, int postL, int postR)
{
if (preL > preR) return NULL;
Node *node = new Node;
node->data = pre[preL];
node->lchild = NULL;
node->rchild = NULL;
if (preL == preR)
return node;
int k = 0;
for (k = preL + 1; k <= preR; k++)
{
if (pre[k] == post[postR - 1]) break;
}
if (k - preL > 1)
{
node->lchild = create(preL + 1, k - 1, postL, postL + k - preL - 2);
node->rchild = create(k, preR, postL + k - preL - 1, postR - 1);
}
else
{
flag = false;
node->rchild = create(k, preR, postL + k - preL - 1, postR - 1);
}
return node;
} void inOrder(Node *node)
{
if (node == NULL) return;
inOrder(node->lchild);
if (index < n - 1)
cout << node->data << " ";
else cout << node->data << endl;
index++;
inOrder(node->rchild);
} int main()
{
cin >> n;
for (int i = 0; i < n; ++i) cin >> pre[i];
for (int i = 0; i < n; ++i) cin >> post[i];
root = create(0, n - 1, 0, n - 1);
if (flag) cout << "Yes\n";
else cout << "No\n";
inOrder(root);
return 0;
}

PAT A 1119. Pre- and Post-order Traversals (30)【二叉树遍历】的更多相关文章

  1. HDU 1710 Binary Tree Traversals (二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  2. HDU 1710 Binary Tree Traversals(二叉树遍历)

    传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...

  3. PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历

    题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...

  4. PAT甲级1119. Pre- and Post-order Traversals

    PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...

  5. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  6. [二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)

    1119. Pre- and Post-order Traversals (30) Suppose that all the keys in a binary tree are distinct po ...

  7. Construct a tree from Inorder and Level order traversals

    Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is a ...

  8. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  9. PAT 1119 Pre- and Post-order Traversals [二叉树遍历][难]

    1119 Pre- and Post-order Traversals (30 分) Suppose that all the keys in a binary tree are distinct p ...

随机推荐

  1. Leetcode 255. Verify Preorder Sequence in Binary Search Tree

    验证一个list是不是一个BST的preorder traversal sequence. Given an array of numbers, verify whether it is the co ...

  2. map、Array工具类,来源于东宏软件

    //===================================================================================package org.jxs ...

  3. 怪物AI之发现玩家(视觉范围发现系列)

    在网上找到一些资料参考,然后写写自己的想法. 这里感谢MOMO等大神. 我们用玩家检测怪物的方法来测,这样比较试用与弱联网游戏,每次在同步玩家的时候来判断玩家与怪物的位置. 这里给出两个处理方式: 1 ...

  4. 加载dll过程中assembly失败

    错误现象: 进行插件读取时出现错误:“尝试从一个网络位置加载程序集,在早期版本的 .NET Framework 中,这会导致对该程序集进行沙盒处理.此发行版的 .NET Framework 默认情况下 ...

  5. 触发器运用示例---laobai

    1 触发器 概念:trigger.逻辑对象的一种.当dml的增删改语句执行时,自动触发一系列动作. 分类:dml触发器.ddl触发器(很少见) sql:ddl,dml,dcl 按触发的时间分: 语句执 ...

  6. d3 document

    https://github.com/d3/d3/wiki/API--%E4%B8%AD%E6%96%87%E6%89%8B%E5%86%8C https://github.com/d3/d3/wik ...

  7. 说一说ASCLL和Unicode

    关于字符编码这个展开来说有太多东西了,这里主要是想说一说最常说的ASCLL和Unicode字符编码的问题,这样至少你在用相关函数的时候,可以搞明白参数的真正含义. ASCLL编码 计算机就是0和1的世 ...

  8. java运行过程

    一.安装环境 大家在开发Java的时候,首先回装一个java的开发环境,一个JDK(也包含了JRE),然后设置环境变量,这个过程我就不细说了,大家装完后有没有发现,在装完这个环境的同时在安装JRE,在 ...

  9. Bash 会清空从父进程继承来的 OLDPWD

    即便 Bash 没有从父进程继承任何的环境变量,Bash 自己也会创建三个环境变量,分别是: $ env -i bash -c export declare -x OLDPWD declare -x ...

  10. SHLVL 和 BASH_SUBSHELL 两个变量的区别

    SHLVL 是记录多个 Bash 进程实例嵌套深度的累加器,而 BASH_SUBSHELL 是记录一个 Bash 进程实例中多个子 Shell(subshell)嵌套深度的累加器. 看不懂上面这句话不 ...