由遍歷序列構造二叉樹

2021-08-07 21:44:07 字數 746 閱讀 2538

根據前序中序確定二叉樹

根據後序中序確定二叉樹

根據層次中序確定二叉樹

注意,根據前序和後序無法確定一棵二叉樹

#include 

#include

#include

#include

#include

#include

using

namespace

std;

#define elemtype char

typedef

struct treenode treenode,* tree;

//遞迴前中後序

void preorder(tree & t)

}void inorder(tree & t)

}void postorder(tree & t)

}void levelorder(tree & t)

}}//根據先序中序建立樹

void preincreate(string pre_str, string in_str, tree & t)

else

t = null;

}//根據後序中序建立樹

void postincreate(string in_str, string post_str, tree & t)

else

t = null;

}int main()

由遍歷序列構造二叉樹

由二叉樹的先序 中序 後序 中序 層次 中序都可以唯一的確定一棵二叉樹。需要注意的是,如果只知道二叉樹的先序序列和後序序列是無法唯一確定一棵二叉樹的。題意 輸入某二叉樹的先序遍歷和中序遍歷結果,請重建出該二叉樹,並輸出它的後序遍歷序列。解題思路 在二叉樹的先序遍歷中,第乙個數字是根結點,在中序遍歷中...

由遍歷序列構造二叉樹

include include include using namespace std define maxsize 100 define maxwidth 40typedef char elemtype typedef struct node btnode btnode createbt1 cha...

二叉樹 根據二叉樹遍歷序列構造二叉樹

二叉樹的節點型別宣告如下 struct btnode 定理1任何 n 0 個不同節點的二叉樹,都可由它的前序序列和中序序列唯一地確定。根據前序遍歷的特點,知前序序列 presequence 的首個元素 presequence 0 為二叉樹的根 root 然後在中序序列 insequence 中查詢此...