/* * 给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。 * 输入格式: * 输入首先给出正整数N(≤50),为树中结点总数。下面两行先后给出先序和中序遍历序列,均是长度为N的不包含重复英文字母(区别大小写)的字符串。 * 输出格式: * 输出为一个整数,即该二叉树的高度。 * 输入样例: * 9 * ABDFGHIEC * FDHGIBEAC * 输出样例: * 5 */ #include"BiTree.h" #include using namespace std; int main() { int n; cout << "请输入节点个数:"; cin >> n; string preorder, inorder; cout << "请输入前置索引:" << endl; cin >> preorder; cout << "请输入后置索引:" << endl; cin >> inorder; std::unordered_map inMap; for (int i = 0; i < inorder.size(); ++i) { inMap[inorder[i]] = i; } BiTree* root = BiTree::buildFromPreorderInorderString(preorder, inorder, 0, n - 1, 0, n - 1, inMap); cout << "树高为:" << BiTree::getHeight(root) << endl; cout << "该数结构为:" << endl << root->toString() << endl; return 0; }