二叉树 三种递归遍历、已知前序中序rebulidBinaryTree

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <stack>
using namespace std;


//Binary Tree Node
typedef struct BTNode {
    char data;
    struct BTNode* pLeftChild;
    struct BTNode* pRightChild;
} BTNode, * PBTNode;

void visit(PBTNode pT) {
    cout << (*pT).data << " ";
}




void preOrder(PBTNode pT) {
    if (pT == NULL) {
        return ;
    }
    visit(pT);
    preOrder(pT->pLeftChild);
    preOrder(pT->pRightChild);
}

void inOrder(PBTNode pT) {
    if (pT == NULL) {
        return ;
    }
    inOrder(pT->pLeftChild);
    visit(pT);
    inOrder(pT->pRightChild);
}

void postOrder(PBTNode pT) {
    if (pT == NULL) {
        return ;
    }
    postOrder(pT->pLeftChild);
    postOrder(pT->pRightChild);
    visit(pT);
}




PBTNode rebulidBinaryTree(string pre, string in, int s1, int e1, int s2, int e2) {
    if (s1 > e1) {
        return NULL;
    }
// pre: 根 左 右
// in: 左 根 右
// post: 左 右 根

    PBTNode pT = (PBTNode)malloc(sizeof(BTNode));
    pT -> pLeftChild = NULL;
    pT -> pRightChild = NULL;
    pT -> data = pre[s1];

    int mid;
    for (int i = s2; i <= e2; i++) {
        if (in[i] == pre[s1]) {
            mid = i;
            break;
        }
    }

    int k1 = mid - s2;
    int k2 = e2 - mid;

    pT -> pLeftChild = rebulidBinaryTree(pre, in, s1 + 1, s1 + k1, s2, mid - 1);
    pT -> pRightChild = rebulidBinaryTree(pre, in, s1 + k1 + 1, e1, mid + 1, e2);

    return pT;
}

int main() {

    string pre, in, post;
    cin >> pre;
    cin >> in;
    PBTNode pT = rebulidBinaryTree(pre, in, 0, pre.size() - 1, 0, in.size() - 1);
    postOrder(pT);
}
#include <iostream>
#include <cstdlib>
using namespace std;

string pre; // 储存前序遍历
string in; // 储存中序遍历

// binary tree node
typedef struct Node {
	char data;
	struct Node* pLeftChild;
	struct Node* pRightChild;
} Node ,* PNode ;

PNode rebulidBinaryTree(string pre,string in,int s1,int e1,int s2,int e2) {
	// 写成了 s1 > e2 多捞啊!! 
	if (s1 > e1) {
		return NULL;
	}

	PNode pT = (PNode)malloc(sizeof(Node));
	pT->data = pre[s1];
	pT->pLeftChild = pT->pRightChild = NULL;

	int mid;
	for (int i = s2; i <= e2; i++) {
		if (in[i] == pre[s1]) {
			mid = i;
			break;
		}
	}
//	cout << "mid is: " << mid << ' ' << in[mid] << endl;

	int k1 = mid - s2;
	int k2 = e2 - mid;
//	cout << "k1  k2 is "<< k1 << " " << k2 << endl;

	pT->pLeftChild = rebulidBinaryTree(pre, in, s1 + 1, s1 + k1, s2, mid - 1);
	pT->pRightChild = rebulidBinaryTree(pre, in, s1 + k1 + 1, e1, mid + 1, e2);

	return pT;
}


void postorder(PNode pT) {
	if (pT == NULL) {
		return ;
	}
	postorder(pT->pLeftChild);
	postorder(pT->pRightChild);
	cout << (*pT).data;
}



int main() {
	while (cin >> pre >> in) {
		PNode pT = rebulidBinaryTree(pre, in, 0, pre.size() - 1, 0, in.size() - 1);

		postorder(pT);
		cout << endl;
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值