Post-order traversal of binary tree (C language)

First, we explain the post-order traversal of binary trees ( recursion + iteration ) from two aspects

1. Post-order traversal of binary tree. (Recursion)

Thought:

First, we traverse its left child from the root node of the binary tree, ① then continue to traverse the left child of its left child until the left child of a left child node is NULL, ② start to traverse its right child, if it is NULL Then visit the value range of the node, and return to its parent node to repeat the operation of the second step, if it is not NULL, repeat the operation of the first step with the node as the root node. End the recursion until all nodes are visited.

Code:

void BTreePostOrder(struct TreeNode* root,int* arry,int* Size){//后序遍历
    if(NULL==root){//递归出口
        return;
    }
    BTreePostOrder(root->left,arry,Size);//遍历左孩子
    BTreePostOrder(root->right,arry,Size);//遍历右孩子
    arry[(*Size)++]=root->val;//访问该节点
}

Running process: (pictured)

2. Post-order traversal (iteration) of binary tree

We should know that it is very simple to use recursion to traverse a binary tree in the pre-in-order and post-order The algorithm framework is very similar, as long as there is one other, it is easy to write), can we write an iterative algorithm: only by changing the order of visiting nodes, we can implement the pre-middle-post-order traversal of the binary tree in an iterative manner, Therefore, we use the stack in the data structure to imitate the recursive form to implement the preorder traversal of the binary tree (access the node value range when pushing into the stack), in-order traversal (access the node value range when popping the stack), this method Pre-order traversal and in-order traversal of iterative level binary trees can be implemented in the same framework, but there is no way to do post-order traversal. After thinking about the relationship between pre-order traversal and post-order traversal, the implementation of pre-order traversal and post-order traversal can be realized in the same framework. Iterative algorithm for in-order traversal.

1. I believe that many people have encountered this kind of problem when they just learned binary trees. Given a binary tree, let us give the node order of the pre-middle-post-order traversal of the binary tree. (Everyone has their own calculation method ), here is my calculation method.

Preorder: We can get its preorder traversal by reading the nodes on the arrow in turn according to the order of the red arrow in the figure and its pointing.

In-order: We can get in-order traversal by reading the nodes on the arrows in the order of the red arrows in the figure and their points.

Post-order: We can get the post-order traversal by reading the nodes on the arrow in the order of the red arrow in the figure and its pointing.

From the above figure, we can see that the post-order traversal of the binary tree is exactly the opposite of the value obtained by the pre-order traversal starting from the right child. Therefore, we can use the code of the pre-order traversal to start the pre-order traversal from the right child, and finally set the The obtained value can be printed in reverse.

Preorder Traversal: Preorder Traversal of Binary Trees (C Language) - Programmer Sought

Postorder Traversal: Inorder Traversal of Binary Trees (C Language) - Programmer Sought

Code:

typedef struct TreeNode BTNode;

typedef struct Stack{//栈的结构体
    BTNode* array[100];
    int size;
}Stack;

void StackPush(Stack* a,BTNode* root){//入栈
    a->array[(a->size)++]=root;
}

void StackPop(Stack* a){//出栈
    (a->size)--;
}

void Reverse(int* a,int Long){//反向打印
    int left_1=0;
    int right_1=Long-1;
    while(left_1 < right_1){
        int temp=a[left_1];
        a[left_1]=a[right_1];
        a[right_1]=temp;
        left_1++;
        right_1--;
    }
}

int* postorderTraversal(struct TreeNode* root, int* returnSize){//从右孩子开始的前序遍历
    int* b=(int*)malloc(sizeof(int)*100);
    if(NULL==b){
        printf("申请节点失败!\n");
        return NULL;
    }
    Stack a;
    a.size=0;
    BTNode* root_temp;
    int i=0;
    StackPush(&a,root);
    while(NULL != a.array[a.size-1]){
        b[i++]=a.array[a.size-1]->val;
        StackPush(&a,a.array[a.size-1]->right);
        while(NULL == a.array[a.size-1]){
            StackPop(&a);
            if(0 == a.size){
                Reverse(b,i);           
                (*returnSize)=i;
                return b;
            }
            root_temp=a.array[a.size-1];
            StackPop(&a); 
            StackPush(&a,root_temp->left);
        }
    }
    Reverse(b,i);
    (*returnSize)=i;
    return b;
}

Preorder traversal starting from the right child: A normal preorder traversal is to visit a node first, then its left child, and then its right child. In this preorder traversal, first visit a node, then its right child, and then its right child. left child.

Code:

void BTreeInOrder(struct TreeNode* root,int* arry,int* Size){//前序遍历
    if(NULL==root){//递归出口
        return;
    }
    arry[(*Size)++]=root->val;//访问该节点
    BTreeInOrder(root->right,arry,Size);//遍历右孩子
    BTreeInOrder(root->left,arry,Size);//遍历左孩子
}

The specific comparison is shown in the figure:

 

Guess you like

Origin blog.csdn.net/weixin_49312527/article/details/122655475