二叉树——重建二叉树
例如,某二叉树为:
前序遍历为:(4,7,9,2,1,5,6)
中序遍历为:(7,9,4,1,2,6,5)
后序遍历为:(9,7,1,6,5,2,4)
1.前序遍历+中序遍历 => 重建二叉树
输入某二叉树的前序遍历和中序遍历的结果,重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
思路
在中序遍历结果中,某个节点前面的节点构成它的左子树,后面的节点构成是它的右子树。因此,对于前序遍历里的某个节点,要判断该节点后面有几个是它的左子树里的节点,可以在中序遍历结果里找到该节点,该节点前面有几个节点则表示左子树里有几个节点。
例如,在这个例子中,前序遍历里的节点4,在中序遍历里它的前面有7、9,说明7、9在它的左子树里,且7是节点4的左孩子;而后面的2,1,5,6则是它的右子树,且节点2是它的右孩子。接下来再对节点4的左子树和右子树进行建树。
代码
#include <iostream>
#include <stdio.h>
#include <string>
#include<string.h>
#include <vector>
#include <map>
#include <stack>
#include <algorithm>
#include <stack>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<int>pre_1;
vector<int>vin_1;
void build(TreeNode* node, int l1, int r1, int l2, int r2) {
if(l1>r1 || l2>r2) {
return;
}
int i, l_ans, r_ans;
int number = pre_1[l1];
for(i=l2; i<=r2; i++) {
if(vin_1[i] == number) {
break;
}
}
l_ans = i - l2;
r_ans = r2 - i;
if(l_ans > 0) {
TreeNode* temp = (TreeNode*)malloc(sizeof(TreeNode));
temp->val = pre_1[l1+1];
temp->left = temp->right = NULL;
node->left = temp;
build(temp, l1+1, l1+l_ans, l2, i-1);
}
if(r_ans > 0) {
TreeNode* temp = (TreeNode*)malloc(sizeof(TreeNode));
temp->val = pre_1[l1+l_ans+1];
temp->left = temp->right = NULL;
node->right = temp;
build(temp, l1+l_ans+1, r1, i+1, r2);
}
}
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
TreeNode *head = NULL;
if(pre.size()==0) {
return head;
}
TreeNode* temp = (TreeNode*)malloc(sizeof(TreeNode));
temp->val = pre[0];
temp->left = temp->right = NULL;
head = temp;
pre_1 = pre;
vin_1 = vin;
build(head, 0, pre.size()-1, 0, vin.size()-1);
return head;
}
};
void display(TreeNode* node) {
if(node==NULL) {
return;
}
display(node->left);
display(node->right);
cout<<node->val<<" ";
}
int main()
{
TreeNode *c;
Solution s;
int a[15] = {1,2,4,7,3,5,6,8};
int b[15] = {4,7,2,1,5,3,8,6};
vector<int> pre(a, a+8);
vector<int> vin(b, b+8);
c = s.reConstructBinaryTree(pre, vin);
display(c);
cout<<endl;
return 0;
}
2.中序遍历+后序遍历 => 重建二叉树
输入某二叉树的中序遍历和后序遍历的结果,重建出该二叉树。假设输入的中序遍历和后序遍历的结果中都不含重复的数字。
代码
#include <iostream>
#include <stdio.h>
#include <string>
#include<string.h>
#include <vector>
#include <map>
#include <stack>
#include <algorithm>
#include <stack>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
//vin, pos
class Solution2 {
public:
vector<int>vin_1;
vector<int>pos_1;
void build(TreeNode* node, int l1, int r1, int l2, int r2) {
if(l1>r1 || l2>r2) {
return ;
}
int i, l_ans, r_ans;
for(i=l1; i<=r1; i++) {
if(vin_1[i] == pos_1[r2]) {
break;
}
}
l_ans = i - l1;
r_ans = r2 - l2 - l_ans;
if(l_ans > 0) {
TreeNode* temp = (TreeNode*)malloc(sizeof(TreeNode));
temp->val = pos_1[l2+l_ans-1];
temp->left = temp->right = NULL;
node->left = temp;
build(temp, l1, i-1, l2, l2+l_ans-1);
}
if(r_ans>0) {
TreeNode* temp = (TreeNode*)malloc(sizeof(TreeNode));
temp->val = pos_1[r2-1];
temp->left = temp->right = NULL;
node->right = temp;
build(temp, i+1, r1, l2+l_ans, r2-1);
}
}
TreeNode* reConstructBinaryTree(vector<int> vin,vector<int> pos) {
TreeNode* head = NULL;
if(pos.size()==0) {
return head;
}
vin_1 = vin;
pos_1 = pos;
TreeNode *temp = (TreeNode*)malloc(sizeof(TreeNode));
temp->val = pos[pos.size()-1];
temp->left = temp->right = NULL;
head = temp;
build(head, 0, vin.size()-1, 0, pos.size()-1);
return head;
}
};
void display(TreeNode* node) {
if(node==NULL) {
return;
}
cout<<node->val<<" ";
display(node->left);
display(node->right);
}
int main()
{
TreeNode *c;
Solution2 s;
int a[15] = {7,9,4,1,2,6,5};
int b[15] = {9,7,1,6,5,2,4};
vector<int> vin(a, a+7);
vector<int> pos(b, b+7);
c = s.reConstructBinaryTree(vin, pos);
display(c);
cout<<endl;
return 0;
}
3.前序遍历+后序遍历 => 无法确定二叉树
当只知道前序遍历和后序遍历时,无法确定一棵二叉树。例如,已知一棵树的前序遍历为(4,7),后序遍历为(7,4),那么这棵树可能的情况有: