Post-order traversal of binary tree--[non-recursive] C language stack implementation

leetcode145 Binary Tree Postorder Traversal

1. Problem description

Given a binary tree, return a postorder traversal of its node values. --- use a non-recursive implementation

2. Problem-solving ideas

Non-recursive post-order traversal --- left and right roots
For a node, to achieve the access order of left son - right son - root node, you can use the LIFO stack, and on the premise that the node is not empty, turn the root , right, left push the stack. Therefore, it is necessary to traverse the tree in the order of root-right-left, and the order of pre-order traversal is root-left-right, so it is only necessary to exchange the left and right of the pre-order traversal and change the access mode printing to push into another stack. . Finally, print the elements in the stack together

step1: put the current node into the temporary stack and the result stack [root] at the same time, and traverse the right subtree----[right]
step2: when the right subtree traversal is completed, pop up the temporary stack The vertex node, and traverse its left subtree----[left], continue step1 step
step3: When all nodes are visited, that is, when the last visited tree node is empty and the temporary stack is also empty, the main algorithm ends , pop out the result stack node in turn

Reference link: https://www.cnblogs.com/llhthinker/p/4747962.html

3. Algorithm Implementation

1. Establishment of stack data structure and definition of binary tree data structure

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

/**Binary tree data structure definition**/
struct TreeNode
{
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
}TreeNode;

/**Stack data structure import**/
#define MAXSIZE 10000
#define OVERFLOW 0
#define error -65530

/**Stack data structure definition**/
typedef struct Sq_stack
{
    struct TreeNode data[MAXSIZE];
    int top;
}Sq_stack;

/**Stack creation --initialization**/
void initStack(Sq_stack *S)
{
    S = (Sq_stack*)malloc(sizeof(Sq_stack));
    if(!S)
        exit(OVERFLOW);//Stack space allocation failed
    S->top = 0; //The top element of the stack starts from 0
}

/**Insert the top element e**/
bool Push(Sq_stack *S, struct TreeNode* e)
{
    /**Insert the top element of the stack: determine whether the stack is full**/
    if( S->top == MAXSIZE-1 )
        return false;
    S->top++;
    S->data[S->top] = *e;
    return true;
}

/**Delete the top element of the stack, and use the node to inherit**/
struct TreeNode* Pop(Sq_stack *S)
{
    /**Delete the top element of the stack: determine whether the stack is empty**/
    if(S->top == 0)
        return NULL;
    struct TreeNode* e = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    *e = S->data[S->top];
    S->top--;
    return e;
}

bool isEmptyStack( Sq_stack *S )
{
    return S->top == 0?true:false;
}

2. Post-order traversal non-recursive implementation algorithm

/****************************************
Author:tmw
date:2018-5-7
****************************************/
/**
*returnSize: used to return the size of the result array
        ---The result is stored in a custom *result array, and its length is passed to *returnSize
**/
int* postorderTraversal(struct TreeNode* root, int* returnSize)
{
    if( root == NULL ) return NULL;

    /**Customize the result array and initialize**/
    int* result = (int*)malloc(1000*sizeof(int));
    int len ​​= 0;

    /**Define stack and initialize**/
    Sq_stack* stk_result = (Sq_stack*)malloc(sizeof(Sq_stack));
    initStack (stk_result);
    Sq_stack* temp_stk = (Sq_stack*)malloc(sizeof(Sq_stack));
    initStack (temp_stk);

    while( root || !isEmptyStack(temp_stk) )
    {
        /** Put the current node into the temporary stack and the result stack [root] at the same time, and traverse the right subtree----[right]**/
        while( root )
        {
            Push(temp_stk,root);
            Push(stk_result,root);
            root = root->right;
        }
        /**When the traversal of the right subtree is completed, pop the top node of the temporary stack stack, and traverse its left subtree----[left], continue while**/
        if( !isEmptyStack(temp_stk) )
        {
            root = Pop(temp_stk);
            root = root->left;
        }
    }
    /**
        When all nodes have been visited, that is, the last visited tree node is empty and the temporary stack is also empty,
        The main algorithm ends, and the result stack nodes are popped out in turn.
    **/
    struct TreeNode* e = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    while( !isEmptyStack(stk_result) )
    {
        e = Pop (stk_result);
        result [len ++] = e-> val;
    }
    free(e);
    *returnSize = len;
    return result;
}

4. Execution results

leetcode accept


There is still a dream, if it comes true~~~ヾ(◍°∇°◍)ノ゙~~~


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325850099&siteId=291194637