資料結構實驗之二叉樹四 (先序中序)還原二叉樹

2021-08-13 14:07:11 字數 692 閱讀 7869

problem description

給定一棵二叉樹的先序遍歷序列和中序遍歷序列,要求計算該二叉樹的高度。

input

輸入資料有多組,每組資料第一行輸入1個正整數n(1 <= n <= 50)為樹中結點總數,隨後2行先後給出先序和中序遍歷序列,均是長度為n的不包含重複英文本母(區分大小寫)的字串。

output

輸出乙個整數,即該二叉樹的高度。

example input

9 

abdfghiec

fdhgibeac

example output

通過先序遍歷序列和中序遍歷序列還原二叉樹,先序遍歷序列的第乙個值肯定為根結點,在中序遍歷序列中找到該值,該值的左側所有的值即為左子樹,右側為右子樹,後分別對左側和右側進行遞迴。

**:#include

#include

using namespace std;

int cnt, k, n;

char a[51], b[51];

struct node

;struct node *create(char a, char b, int n)

int deep(struct node *root)

return d;

}int main()

return 0;

}

資料結構實驗之二叉樹四 (先序中序)還原二叉樹

time limit 1000ms memory limit 65536kb submit statistic problem description 給定一棵二叉樹的先序遍歷序列和中序遍歷序列,要求計算該二叉樹的高度。input 輸入資料有多組,每組資料第一行輸入1個正整數n 1 n 50 為樹中...

資料結構實驗之二叉樹四 (先序中序)還原二叉樹

time limit 1000ms memory limit 65536kb submit statistic problem description 給定一棵二叉樹的先序遍歷序列和中序遍歷序列,要求計算該二叉樹的高度。input 輸入資料有多組,每組資料第一行輸入1個正整數n 1 n 50 為樹中...

資料結構實驗之二叉樹四 (先序中序)還原二叉樹

time limit 1000ms memory limit 65536kb submit statistic problem description 給定一棵二叉樹的先序遍歷序列和中序遍歷序列,要求計算該二叉樹的高度。input 輸入資料有多組,每組資料第一行輸入1個正整數n 1 n 50 為樹中...