time limit: 1000ms
memory limit: 65536 kb
submit
statistic
problem description
給定一棵二叉樹的先序遍歷序列和中序遍歷序列,要求計算該二叉樹的高度。
input
輸入資料有多組,每組資料第一行輸入
1個正整數
n(1 <= n <= 50)
為樹中結點總數,隨後
2行先後給出先序和中序遍歷序列,均是長度為
n的不包含重複英文本母
(區分大小寫
)的字串。
output
輸出乙個整數,即該二叉樹的高度。
example input
9abdfghiec
fdhgibeac
example output
5
#include#include#includeusing namespace std;
struct node
;struct node *creat(int n,char a,char b)
t=p-b;
root->l=creat(t,a+1,b);
root->r=creat(n-t-1,a+1+t,p+1);
return root;
};int deepth(struct node *root)
return d;
}int main()
;struct node *creat(int n,char a,char b)
t=p-b;
root->l=creat(t,a+1,b);
root->r=creat(n-t-1,a+t+1,p+1);
}return root;
};int deep(struct node *root)
return d;
}int main()
return 0;
}
資料結構實驗之二叉樹四 還原二叉樹
time limit 1000ms memory limit 65536k 給定一棵二叉樹的先序遍歷序列和中序遍歷序列,要求計算該二叉樹的高度。輸入資料有多組,每組資料第一行輸入1個正整數n 1 n 50 為樹中結點總數,隨後2行先後給出先序和中序遍歷序列,均是長度為n的不包含重複英文本母 區分大小...
資料結構實驗之二叉樹四 還原二叉樹
time limit 1000ms memory limit 65536k 有疑問?點這裡 給定一棵二叉樹的先序遍歷序列和中序遍歷序列,要求計算該二叉樹的高度。輸入資料有多組,每組資料第一行輸入 1個正整數 n 1 n 50 為樹中結點總數,隨後 2行先後給出先序和中序遍歷序列,均是長度為 n的不包...
資料結構實驗之二叉樹四 還原二叉樹
給定一棵二叉樹的先序遍歷序列和中序遍歷序列,要求計算該二叉樹的高度。輸入資料有多組,每組資料第一行輸入 1個正整數 n 1 n 50 為樹中結點總數,隨後 2行先後給出先序和中序遍歷序列,均是長度為 n的不包含重複英文本母 區分大小寫 的字串。輸出乙個整數,即該二叉樹的高度。9 abdfghiec ...