已知後序中序,求先序

2021-10-09 04:01:51 字數 848 閱讀 4855

利用後序遍歷的最後乙個元素(根結點),在中序遍歷中找到它,分割為左子樹和右子樹,然後在後序遍歷中找到子樹的根結點、起點、終點,分別遞迴。

int leftrootpo=rootpo-(end-rootin)-1; //左子樹的根結點在後序遍歷中的位置。(end-rootin):右子樹的結點個數

int leftstart=start; //左子樹在中序遍歷的開始位置

int leftend=rootin-1; //左子樹在中序遍歷的結束位置

//cout<

int rightrootpo=rootpo-1;

int rightstart=rootin+1;

int rightend=end;

//cout<

pre(leftrootpo,leftstart,leftend); //遞迴左子樹

pre(rightrootpo,rightstart,rightend); //遞迴右子樹

}

已知中序和後序求先序

include include using namespace std struct tnode define len sizeof tnode typedef tnode pbtnodepoint void vcreate string sinorder,string spostorder,pbt...

已知後序跟中序求先序

分類 資料結構 2013 11 28 19 52 7人閱讀收藏 舉報而已知後序遍歷和中序遍歷求前序遍歷的過程差不多,但由於後序遍歷是最後才訪問根節點的 所以要從後開始搜尋,例如上面的例子,後序遍歷為 gbdehfca,中序遍歷為 dgbaechf 後序遍歷中的最後乙個元素是根節點,a,然後查詢中序中...

已知先序和中序求後序

輸入二叉樹的先序遍歷序列和中序遍歷序列,輸出該二叉樹的後序遍歷序列。input 第一行輸入二叉樹的先序遍歷序列 第二行輸入二叉樹的中序遍歷序列。output 輸出該二叉樹的後序遍歷序列。sample input abdcef bdaecf sample output dbefca include i...