#include<iostream>
#include<vector>
#include<deque>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> in)
{
int inlen = in.size();
if (inlen == 0)
return NULL;
vector<int> left_pre, right_pre, left_in, right_in;
//创建根节点,根节点肯定是前序遍历的第一个数
TreeNode* head = new TreeNode(pre[0]);
//找到中序遍历根节点所在位置,存放于变量gen中
int gen = 0;
for (int i = 0; i<inlen; i++)
{
if (in[i] == pre[0])
{
gen = i;
break;
}
}
//对于中序遍历,根节点左边的节点位于二叉树的左边,根节点右边的节点位于二叉树的右边
//利用上述这点,对二叉树节点进行归并
for (int i = 0; i<gen; i++)
{
left_in.push_back(in[i]);
left_pre.push_back(pre[i + 1]);//前序第一个为根节点
}
for (int i = gen + 1; i<inlen; i++)
{
right_in.push_back(in[i]);
right_pre.push_back(pre[i]);
}
//和shell排序的思想类似,取出前序和中序遍历根节点左边和右边的子树
//递归,再对其进行上述所有步骤,即再区分子树的左、右子子数,直到叶节点
head->left = reConstructBinaryTree(left_pre, left_in);
head->right = reConstructBinaryTree(right_pre, right_in);
return head;
}
vector<int> PrintFromTopToBottom(TreeNode* root)//按层次来遍历
{
deque<TreeNode*>q;
vector<int>res;
if (root == NULL)return res;
q.push_back(root);
while (!q.empty())
{
TreeNode* pnode = q.front();
res.push_back(pnode->val);
if (pnode->left != NULL) q.push_back(pnode->left);
if (pnode->right != NULL)q.push_back(pnode->right);
q.pop_front();
}
return res;
}
vector<int>res,res_in,res_be;//在前序遍历或者后序遍历中,不能定义为局部变量,否则每次递归实现时,res会每次被初始化,导致他的大小为1
vector<int> PrintFromTopToBottompre(TreeNode* root)//按前序来遍历
{
if (root == NULL)return res;
else
{
//cout << root->val << endl;
res.push_back(root->val);
PrintFromTopToBottompre(root->left);
PrintFromTopToBottompre(root->right);
}
//cout << res.size();
return res;
}
vector<int> PrintFromTopToBottomin(TreeNode* root)//按zhong序来遍历
{
if (root == NULL)return res_in;
else
{
PrintFromTopToBottomin(root->left);
res_in.push_back(root->val);
PrintFromTopToBottomin(root->right);
}
//cout << res.size();
return res_in;
}
vector<int> PrintFromTopToBottombe(TreeNode* root)//按前序来遍历
{
if (root == NULL)return res_be;
else
{
//cout << root->val << endl;
PrintFromTopToBottombe(root->left);
PrintFromTopToBottombe(root->right);
res_be.push_back(root->val);
}
//cout << res.size();
return res_be;
}
int main()
{
vector<int> pre;
pre.push_back(1);
pre.push_back(2);
pre.push_back(4);
pre.push_back(7);
pre.push_back(3);
pre.push_back(5);
pre.push_back(6);
pre.push_back(8);
vector<int> vin;
vin.push_back(4);
vin.push_back(7);
vin.push_back(2);
vin.push_back(1);
vin.push_back(5);
vin.push_back(3);
vin.push_back(8);
vin.push_back(6);
TreeNode* root=reConstructBinaryTree(pre, vin);
vector<int> re;
//re = PrintFromTopToBottom(root);
/*re=PrintFromTopToBottompre(root);
for (int i = 0; i < re.size(); i++)
cout << re[i] << endl;*/
re = PrintFromTopToBottomin(root);
for (int i = 0; i < re.size(); i++)
cout << re[i] << endl;
system("pause");
return 0;
}
#include<vector>
#include<deque>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> in)
{
int inlen = in.size();
if (inlen == 0)
return NULL;
vector<int> left_pre, right_pre, left_in, right_in;
//创建根节点,根节点肯定是前序遍历的第一个数
TreeNode* head = new TreeNode(pre[0]);
//找到中序遍历根节点所在位置,存放于变量gen中
int gen = 0;
for (int i = 0; i<inlen; i++)
{
if (in[i] == pre[0])
{
gen = i;
break;
}
}
//对于中序遍历,根节点左边的节点位于二叉树的左边,根节点右边的节点位于二叉树的右边
//利用上述这点,对二叉树节点进行归并
for (int i = 0; i<gen; i++)
{
left_in.push_back(in[i]);
left_pre.push_back(pre[i + 1]);//前序第一个为根节点
}
for (int i = gen + 1; i<inlen; i++)
{
right_in.push_back(in[i]);
right_pre.push_back(pre[i]);
}
//和shell排序的思想类似,取出前序和中序遍历根节点左边和右边的子树
//递归,再对其进行上述所有步骤,即再区分子树的左、右子子数,直到叶节点
head->left = reConstructBinaryTree(left_pre, left_in);
head->right = reConstructBinaryTree(right_pre, right_in);
return head;
}
vector<int> PrintFromTopToBottom(TreeNode* root)//按层次来遍历
{
deque<TreeNode*>q;
vector<int>res;
if (root == NULL)return res;
q.push_back(root);
while (!q.empty())
{
TreeNode* pnode = q.front();
res.push_back(pnode->val);
if (pnode->left != NULL) q.push_back(pnode->left);
if (pnode->right != NULL)q.push_back(pnode->right);
q.pop_front();
}
return res;
}
vector<int>res,res_in,res_be;//在前序遍历或者后序遍历中,不能定义为局部变量,否则每次递归实现时,res会每次被初始化,导致他的大小为1
vector<int> PrintFromTopToBottompre(TreeNode* root)//按前序来遍历
{
if (root == NULL)return res;
else
{
//cout << root->val << endl;
res.push_back(root->val);
PrintFromTopToBottompre(root->left);
PrintFromTopToBottompre(root->right);
}
//cout << res.size();
return res;
}
vector<int> PrintFromTopToBottomin(TreeNode* root)//按zhong序来遍历
{
if (root == NULL)return res_in;
else
{
PrintFromTopToBottomin(root->left);
res_in.push_back(root->val);
PrintFromTopToBottomin(root->right);
}
//cout << res.size();
return res_in;
}
vector<int> PrintFromTopToBottombe(TreeNode* root)//按前序来遍历
{
if (root == NULL)return res_be;
else
{
//cout << root->val << endl;
PrintFromTopToBottombe(root->left);
PrintFromTopToBottombe(root->right);
res_be.push_back(root->val);
}
//cout << res.size();
return res_be;
}
int main()
{
vector<int> pre;
pre.push_back(1);
pre.push_back(2);
pre.push_back(4);
pre.push_back(7);
pre.push_back(3);
pre.push_back(5);
pre.push_back(6);
pre.push_back(8);
vector<int> vin;
vin.push_back(4);
vin.push_back(7);
vin.push_back(2);
vin.push_back(1);
vin.push_back(5);
vin.push_back(3);
vin.push_back(8);
vin.push_back(6);
TreeNode* root=reConstructBinaryTree(pre, vin);
vector<int> re;
//re = PrintFromTopToBottom(root);
/*re=PrintFromTopToBottompre(root);
for (int i = 0; i < re.size(); i++)
cout << re[i] << endl;*/
re = PrintFromTopToBottomin(root);
for (int i = 0; i < re.size(); i++)
cout << re[i] << endl;
system("pause");
return 0;
}