(Leetcode) binary tree preorder traversal -c language

Given a binary tree, it returns  to the preamble  traversal.

 Example:

Input: [. 1, null, 2,3]   
   . 1 
    \ 
     2 
    / 
   . 3 

Output: [1,2,3]

Advanced: recursive algorithm is very simple, you can do it through an iterative algorithm?

Preorder traversal


Preorder traversal first visit the root, then traverse left subtree, finally traversing the right subtree.

C language to achieve too much trouble, I thought, first of all subject must first achieve a pre-order traversal probably lower now introduce, if recursion would be relatively simple, a few lines of code can be achieved, but now requires the use of iterative development to achieve. Traversing the entire process, access the root node, and then traverse the left subtree, then see if the left subtree has its left child and right child. Because after viewing the left child, right child would go to see the root, so each time you need to root record, require the presence of the stack. So we need to implement a stack with push and pop operations. In addition, we need to store a list of nodes already visited, in the end, these need to be unified storage nodes into an array, and then return.

Let's look at my code code

/ * List node for storing the output * / 
struct listnode {
     int Val;
     struct listnode * Next; 
}; 

struct List {
     int COUNT;
     struct listnode * head;
     struct listnode * tail; 
}; 

/ * stack node, for storing has traversed the root node * / 
struct StackNode 
{ 
    void * entry;
     struct StackNode * Next; 
}; 

struct Stack {
     struct StackNode * Top; 
}; 

void init_stack(struct stack *s)
{
    s->top = NULL;
}

void stack_push(struct stack *s, void *np)
{
    struct StackNode *node = malloc(sizeof(struct StackNode));
    node->entry = np;
    node->next = s->top;
    s->top = node;
};

void *stack_pop(struct stack *s)
{
    struct StackNode *np = s->top;
    void *node = np->entry;
    s->top = np->next;
    free(np);
    return node;
};

bool isEmpty(struct stack *s)
{
    return (s->top == NULL) ? true : false;
}

void init_list(struct list *l)
{
    l->count = 0;
    l->head = NULL;
    l->tail = NULL;
}

void add_new_node(struct list *l, struct listNode *node)
{
    if (!l->head)
    {
        l->head = node;
        l->tail = node;
        l->count = 1;
        return;
    }
    
    l->tail->next = node;
    l->tail = node;
    l->count++;
}

These are the helpers

int* preorderTraversal(struct TreeNode* root, int* returnSize){
    struct TreeNode *pNode = root;
    struct listNode *newNode = NULL;
    struct list *l = malloc(sizeof(struct list));
    struct stack *s = malloc(sizeof(struct stack));
    int *r = NULL;
    int i = 0;
    struct listNode *head = NULL;
    init_list(l);
    init_stack(s);
    
    while (pNode != NULL || !isEmpty(s))
    {
        if (pNode != NULL)
        {
            newNode = malloc(sizeof(struct listNode));
            newNode->val = pNode->val;
            newNode->next = NULL;
            add_new_node(l, newNode);
            stack_push(s, (void *)pNode);
            pNode = pNode->left;
        }
        else
        {
            pNode = (struct TreeNode *)stack_pop(s);
            pNode = pNode->right;            
        }        
    }
    
    r = malloc(sizeof(int) * l->count);
    head = l->head;
    while(head && i < l->count)
    {
        r[i] = head->val;
        i++;
        head = head->next;
    }
    *returnSize = l->count;
    
    return r;    
}

This is the specific function of a preorder traversal.

Guess you like

Origin www.cnblogs.com/xingmuxin/p/11278004.html