首页 > TAG信息列表 > postR

1020 Tree Traversals (25 分)/中序遍历+后序遍历转为层次遍历

#include<iostream> #include<queue> #include<algorithm> #include<stdio.h> using namespace std; struct node { int data; node* lchild; node* rchild; }; const int maxn=50; int n,pre[maxn],post[maxn],in[maxn]; node* create

PAT 1127 ZigZagging on a Tree

题解: 代码: #include<iostream> #include<vector> using namespace std; const int maxv=39; vector<int> tree[maxv]; int in[maxv],post[maxv]; int layer=1; void layertravel(int postL,int postR,int inL,int inR,int nowlayer){ if(layer<nowlaye

PATA 1020 Tree Traversals

题目大意: 给定二叉树的后根序列和中根序列,输出层次序列。 输入: 第一行:节点个数 第二行:后根序列 第三行:中根序列 输出: 层次序列,数字间用一个空格隔开,末尾不允许有多余空格。 代码: #include <stdio.h> #include <queue> using namespace std; const int maxn=40; int post[m

根据中序、后续遍历序列构建二叉树

算法思想   后序遍历序列的最后一个节点是根节点,而中序遍历的根节点左侧都是左子树的节点,右侧都是右子树的节点,根据这种特点,我们可以从后序遍历中找到根节点,再在中序遍历中找到根节点,然后递归的对中序遍历的左子树及右子树进行建树; 代码 struct TreeNode { int val;

【LeetCode】106. 从中序与后序遍历序列构造二叉树

解题思路 类似于通过前序遍历和中序遍历思想 由于后序遍历的最后一个元素一定是根节点,因此容易获得根节点,再通过中序遍历,可以知道左右子树的位置及个数 对于中序遍历的左右子树,再递归进行构建 代码 class Solution { public: TreeNode* buildTree(vector<int>& inorder, ve