一.前序遍历
(1)先访问左子树。(2)再访问根节点。(3)最后访问右子树。
上面这个二叉树的前序遍历结果是:ABDECFG.
官方解法:
(1)递归法:
void preorder(struct TreeNode* root, int* res, int* resSize) {
if (root == NULL) {
return;
}
res[(*resSize)++] = root->val;
preorder(root->left, res, resSize);
preorder(root->right, res, resSize);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
int* res = malloc(sizeof(int) * 2000);
*returnSize = 0;
preorder(root, res, returnSize);
return res;
}
(2)迭代法:
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
int* res = malloc(sizeof(int) * 2000);
*returnSize = 0;
if (root == NULL) {
return res;
}
struct TreeNode* stk[2000];
struct TreeNode* node = root;
int stk_top = 0;
while (stk_top > 0 || node != NULL) {
while (node != NULL) {
res[(*returnSize)++] = node->val;
stk[stk_top++] = node;
node = node->left;
}
node = stk[--stk_top];
node = node->right;
}
return res;
}
二.中序遍历
(1)先访问左子树。(2)再访问根节点。(3)最后访问右子树。
上面这个二叉树的中序遍历结果是:DBEAFCG.
官方解法:
(1)递归法
void inorder(struct TreeNode* root, int* res, int* resSize) {
if (!root) {
return;
}
inorder(root->left, res, resSize);
res[(*resSize)++] = root->val;
inorder(root->right, res, resSize);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
int* res = malloc(sizeof(int) * 501);
*returnSize = 0;
inorder(root, res, returnSize);
return res;
}
(2)迭代法
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
*returnSize = 0;
int* res = malloc(sizeof(int) * 501);
struct TreeNode** stk = malloc(sizeof(struct TreeNode*) * 501);
int top = 0;
while (root != NULL || top > 0) {
while (root != NULL) {
stk[top++] = root;
root = root->left;
}
root = stk[--top];
res[(*returnSize)++] = root->val;
root = root->right;
}
return res;
}
三.二叉树的后序遍历
(1)先访问左子树。(2)再访问右子树。(3)最后访问根节点。
上面这个二叉树的后序遍历结果是:DEBFGCA.
官方解法:
(1)递归法
void postorder(struct TreeNode *root, int *res, int *resSize) {
if (root == NULL) {
return;
}
postorder(root->left, res, resSize);
postorder(root->right, res, resSize);
res[(*resSize)++] = root->val;
}
int *postorderTraversal(struct TreeNode *root, int *returnSize) {
int *res = malloc(sizeof(int) * 2001);
*returnSize = 0;
postorder(root, res, returnSize);
return res;
}
(2)迭代法
int *postorderTraversal(struct TreeNode *root, int *returnSize) {
int *res = malloc(sizeof(int) * 2001);
*returnSize = 0;
if (root == NULL) {
return res;
}
struct TreeNode **stk = malloc(sizeof(struct TreeNode *) * 2001);
int top = 0;
struct TreeNode *prev = NULL;
while (root != NULL || top > 0) {
while (root != NULL) {
stk[top++] = root;
root = root->left;
}
root = stk[--top];
if (root->right == NULL || root->right == prev) {
res[(*returnSize)++] = root->val;
prev = root;
root = NULL;
} else {
stk[top++] = root;
root = root->right;
}
}
return res;
}
三种遍历方式
typedef struct Tree{
char data; // 存放数据域
struct Tree *Lnode; // 遍历左子树指针
struct Tree *Rnode; // 遍历右子树指针
}Tree,*BitTree;
//二叉树的先序遍历
void PreOrderTraverse(BitTree T)
{
if(T == NULL){
return;
}
printf("%c ",T -> data);
PreOrderTraverse(T -> Lnode);
PreOrderTraverse(T -> Rnode);
}
//二叉树的中序遍历
void InOrderTraverse(BitTree T)
{
if(T == NULL){
return;
}
InOrderTraverse(T -> Lnode);
printf("%c ",T -> data);
InOrderTraverse(T -> Rnode);
}
//二叉树的后序遍历
void PostOrderTraverse(BitTree T)
{
if(T == NULL){
return;
}
PostOrderTraverse(T -> Lnode);
PostOrderTraverse(T -> Rnode);
printf("%c ",T -> data);
}