//递归法遍历二叉树
void btree ::visit(node * searchp){
cout<<setw(3)<<serchp->data;
}
void btree::preorder(node *searchp)
{
if(searchp != NULL){
visit(searchp);
preorder(searchp->lchild);
preorder(searchp->rchild);
}
}
//非递归先根遍历二叉树
returninfo btree::nrpreorder(node *searchp){
node *stack[Maxsize],*pnow;
searchp = root;
getcount();
while(!(pnow == NULL && stop == 0)){
while(pnow != NULL){
btreecount--;
visit(pnow);
if(top < Maxsize - 1){
stack[top] = pnow;
top++
}
else
return overflow;
pnow = pnow ->lchild;
}
if(top <= 0)
return success;
else{
top--;
pnow = stack[top];
pnow = pnow->rchild;
}
}
}
return success;
}
//非递归后根遍历二叉树--由于必须把两个孩子都访问完才可以访问根,为了保留返回地址,所以必然进栈两次。所以约定
//节点要两次进栈,两次出栈,而访问节点在第二次出栈时访问。
class stackdata{
friend class btree;
private:
node *link;
int flag;//flag=1,代表第一次进栈,不能访问。flag=2,代表第二次进栈,可以访问
public:
stackdata() {};
~stackdata() {};
};
returninfo btree::nrpostorder(node *searchp){
stackdata stack[Maxsize];
node *pnow;
int top,sign;
serachp = root;
if(serachp == NULL)
return underflow;
top = -1;
getcount();
while(!(pnow == NULL && stop = -1)){
if(pnow != NULL){
if(top < Maxsize - 1){
top ++
stack[top].link = pnow;
stack[top].flag = 1;
pnow = pnow ->lchild;
}
else
return overflow;
}
else{
pnow = stack[top].link;
sign = stack[top].flag;
top --;
if(sign == 1){
top++;
stack[top].link = pnow;
stack[top].flag = 2;
pnow = pnow->rchild;
}
else{
btreecount --;
visit(pnow);
pnow = NULL;
}
}
}
return success;
}
// 二叉树的层次遍历
returninfo btree :: levelorder(node *searchp){
node *queue[Maxsize];
int front,rear;
searchp = root;
if(searchp = NULL)
return underflow;
getcount();
front = -1;
rear = 0;
queue[rear] = searchp;
while(front != rear){
front++;
btreecount--;
visit(queue[front]);
if(queue[front]->lchild != NULL){
rear++;
queue[rear] = queue[front]->lchild;
}
if(queue[front]->rchild != NULL){
rear++;
queue[rear] = queue[front]->rchild;
}
}
return success;
}
//给一棵带权的二叉树的中序和后序遍历,找一个叶子使它到根的路径上的权和最小。--根据后序遍历找到树根,然后在中序遍历中
//找到树根,然后找出左右子树的结点列表,然后递归构造左右子树
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
const int maxv = 10010;
int int_order[maxv], postorder[maxv], lch[maxv],rch[maxv];
int n;
bool read_list(int *a){
string line;
if(!getline(cin,line))
return false;
stringstream ss(line);
n = 0;
int = x;
while(ss>>x)
a[n++] = x;
return 0;
}
//构造二叉树
int build(int L1,int R1,int L2,int R2){
if(L1 > R1)
return 0;//空树
int root = post_order[R2];
int p = L1;
while(in_order[p] != root)
p++;
int cnt = p - L1;
lch[root] = build(L1,p-1,L2,L2+cnt-1);
rch[root] = build(p+1,R1,L2+cnt,R2-1);
return root;
}
int best,best_sum;
void dfs(int u,int sum){
sum += u;
if(!lch[u] && !rch[u]){
if(sum<best_sum || (sum == best_sum && u < best)){
best = u;
best_sum = sum;
}
}
if(lch[u])
dfs(lch[u],sum);
if(rch[u])
dfs(rch[u],sum);
}
int main(){
while(read_list(in_order)){
read_list(post_order);
build(0,n-1,0,n-1);
best_sum = 100000000;
dfs(post_order[n-1],0);
cout<<best;
}
return 0;
}
void btree ::visit(node * searchp){
cout<<setw(3)<<serchp->data;
}
void btree::preorder(node *searchp)
{
if(searchp != NULL){
visit(searchp);
preorder(searchp->lchild);
preorder(searchp->rchild);
}
}
//非递归先根遍历二叉树
returninfo btree::nrpreorder(node *searchp){
node *stack[Maxsize],*pnow;
searchp = root;
getcount();
while(!(pnow == NULL && stop == 0)){
while(pnow != NULL){
btreecount--;
visit(pnow);
if(top < Maxsize - 1){
stack[top] = pnow;
top++
}
else
return overflow;
pnow = pnow ->lchild;
}
if(top <= 0)
return success;
else{
top--;
pnow = stack[top];
pnow = pnow->rchild;
}
}
}
return success;
}
//非递归后根遍历二叉树--由于必须把两个孩子都访问完才可以访问根,为了保留返回地址,所以必然进栈两次。所以约定
//节点要两次进栈,两次出栈,而访问节点在第二次出栈时访问。
class stackdata{
friend class btree;
private:
node *link;
int flag;//flag=1,代表第一次进栈,不能访问。flag=2,代表第二次进栈,可以访问
public:
stackdata() {};
~stackdata() {};
};
returninfo btree::nrpostorder(node *searchp){
stackdata stack[Maxsize];
node *pnow;
int top,sign;
serachp = root;
if(serachp == NULL)
return underflow;
top = -1;
getcount();
while(!(pnow == NULL && stop = -1)){
if(pnow != NULL){
if(top < Maxsize - 1){
top ++
stack[top].link = pnow;
stack[top].flag = 1;
pnow = pnow ->lchild;
}
else
return overflow;
}
else{
pnow = stack[top].link;
sign = stack[top].flag;
top --;
if(sign == 1){
top++;
stack[top].link = pnow;
stack[top].flag = 2;
pnow = pnow->rchild;
}
else{
btreecount --;
visit(pnow);
pnow = NULL;
}
}
}
return success;
}
// 二叉树的层次遍历
returninfo btree :: levelorder(node *searchp){
node *queue[Maxsize];
int front,rear;
searchp = root;
if(searchp = NULL)
return underflow;
getcount();
front = -1;
rear = 0;
queue[rear] = searchp;
while(front != rear){
front++;
btreecount--;
visit(queue[front]);
if(queue[front]->lchild != NULL){
rear++;
queue[rear] = queue[front]->lchild;
}
if(queue[front]->rchild != NULL){
rear++;
queue[rear] = queue[front]->rchild;
}
}
return success;
}
//给一棵带权的二叉树的中序和后序遍历,找一个叶子使它到根的路径上的权和最小。--根据后序遍历找到树根,然后在中序遍历中
//找到树根,然后找出左右子树的结点列表,然后递归构造左右子树
#include<iostream>
#include<string>
#include<sstream>
#include<algorithm>
using namespace std;
const int maxv = 10010;
int int_order[maxv], postorder[maxv], lch[maxv],rch[maxv];
int n;
bool read_list(int *a){
string line;
if(!getline(cin,line))
return false;
stringstream ss(line);
n = 0;
int = x;
while(ss>>x)
a[n++] = x;
return 0;
}
//构造二叉树
int build(int L1,int R1,int L2,int R2){
if(L1 > R1)
return 0;//空树
int root = post_order[R2];
int p = L1;
while(in_order[p] != root)
p++;
int cnt = p - L1;
lch[root] = build(L1,p-1,L2,L2+cnt-1);
rch[root] = build(p+1,R1,L2+cnt,R2-1);
return root;
}
int best,best_sum;
void dfs(int u,int sum){
sum += u;
if(!lch[u] && !rch[u]){
if(sum<best_sum || (sum == best_sum && u < best)){
best = u;
best_sum = sum;
}
}
if(lch[u])
dfs(lch[u],sum);
if(rch[u])
dfs(rch[u],sum);
}
int main(){
while(read_list(in_order)){
read_list(post_order);
build(0,n-1,0,n-1);
best_sum = 100000000;
dfs(post_order[n-1],0);
cout<<best;
}
return 0;
}