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

leetcode94 Binary Tree inorder Traversal

1. Problem description

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

【Example】

input [1,null,2,3]
   1
    \
     2
    /
   3

output [1,2,3]

2. Problem-solving ideas

Non-recursive implementation -- using stack
in -order traversal: left root right

Starting from the current node T, push the stack -> T=T->left, the left subtree keeps pushing the stack until it traverses to the deepest left subtree [left] -> pop the current node pop(T) -> if the current If the node has a right subtree, then T=T->right, and the right subtree is pushed to the stack [Right]->If there is currently a left subtree, the left subtree is always pushed to the stack, and so on.

3. Code 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);//栈空间分配失败
    S->top = 0; //栈顶元素从0开始算起
}

/**插入栈顶元素e**/
bool Push(Sq_stack *S, struct TreeNode* e)
{
    /**插入栈顶元素:判断栈是否已满**/
    if( S->top == MAXSIZE-1 )
        return false;
    S->top++;
    S->data[S->top] = *e;
    return true;
}

/**删除栈顶元素,并用节点承接**/
struct TreeNode* Pop(Sq_stack *S)
{
    /**删除栈顶元素:判断栈是否为空**/
    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、前序遍历非递归实现算法

/******************************************
Author:tmw
date:2018-5-7
******************************************/
/**
*returnSize:用于返回结果数组的大小
        ---The result is stored in a custom *result array, and its length is passed to *returnSize
**/
int* inorderTraversal(struct TreeNode* root, int* returnSize)
{
    if( root == NULL ) return NULL;

    /**Custom result array and length**/
    int* result = (int*)malloc(1000*sizeof(int));
    int len ​​= 0;

    /**Define stack and initialize**/
    Sq_stack* stk = (Sq_stack*)malloc(sizeof(Sq_stack));
    initStack (pcs);

    /**Inorder traversal--non-recursive implementation of the main algorithm part**/
    while( root || !isEmptyStack(stk) )
    {
        /**The current node (left subtree) exists, then the left subtree continues to be pushed onto the stack**/
        while( root )
        {
            Push(stk,root);
            root = root->left; /**左**/
        }
        /**
            All the left subtrees below the current node are pushed into the stack, the top element (the deepest left subtree) of the stack is popped out, and the node value is included in the result array.
            And find the right subtree of the element, if there is, enter the previous while loop and push the stack and find the left subtree of the current right subtree.
        **/
        if (! isEmptyStack (pcs))
        {
            root = Pop(stk); /**root**/
            result[len++] = root->val;
            root = root->right; /**右**/
        }
    }
    *returnSize = len;
    return result;
}

4. Execution results

leetcode C accept

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

Guess you like

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