Preorder traversal of binary tree (C language)

We explain the preorder traversal of binary trees from two directions ( recursion + iteration )

1. Recursion

Thought:

Traversing from the root node to its left child, record the value of the node every time it passes through the node (only when it passes the node for the first time), return to the previous node when it reaches NULL, and then traverse its right child, if The right child exists to record its value, and then repeat the above operation, if it does not exist, return to the previous node and repeat the above operation.

code show as below:

void BTreePrevOrder(struct TreeNode* root,int* a,int* returnSize){//前序遍历
    if(NULL==root){//判出条件
        return;
    }
    a[(*returnSize)++]=root->val;
    BTreePrevOrder(root->left,a,returnSize);
    BTreePrevOrder(root->right,a,returnSize);
}

The specific operation process: (as shown in the figure)

2. Iteration

Thought:

First of all, we need to know that the recursive process is a process of stacking and popping, so we can use iteration to imitate the process of stacking and popping to achieve preorder traversal of the binary tree. First, determine whether the binary tree is empty or not. Return, if it is not empty, perform preorder traversal, push the root node into the stack, record the value of the first element of the stack, and then push its left child into the stack. When the first element of the stack is empty at a certain time, enter the loop and first put the empty pops the node from the stack, then records the new first element of the stack and pops it from the stack. At this time, the first element of the stack is the element of the previous layer, which is convenient for it to return to the previous layer, and then pushes the right child of the recorded node into the stack, which is Empty to continue the cycle, not empty to exit the cycle to repeat the above operations. (For the specific process, see the figure below)

code show as below:

​
typedef struct TreeNode BTNode;

typedef struct Stack{//构建栈的结构体
    BTNode* root_a[100];//存二叉树的节点的数组
    int size;//表示存储个数
}Stack;

void PushStack(Stack* b,BTNode* root){//入栈
    b->root_a[b->size++]=root;
}

void PopStack(Stack* b){//出栈
    b->size--;
}

int* preorderTraversal(struct TreeNode* root, int* returnSize){
    int* a=(int*)malloc(sizeof(int)*100);//创建数组用来存储二叉树前序遍历的值
    if(NULL==a){
        printf("申请节点失败!\n");
        return NULL;
    }
    Stack b;//创建栈
    BTNode* pop_temp;//用来记录将要出栈的节点
    b.size=0;
    int i=0;
    PushStack(&b,root);//先把根节点入栈
    while(NULL != b.root_a[b.size-1]){
        a[i++]=b.root_a[b.size-1]->val;//第一次访问该节点时记录其值
        PushStack(&b,b.root_a[b.size-1]->left);//将该节点的左孩子入栈如果为NULL进入下一个
//循环
        while(NULL == b.root_a[b.size-1]){
            PopStack(&b);//先把这个为NULL的节点出栈
            if(0==b.size){//如果栈为空则遍历结束退出循环
                (*returnSize)=i;
                return a;                
            }
            pop_temp=b.root_a[b.size-1];//记录上一个节点
            PopStack(&b);//将其出栈,使其返回时可以直接进入上一层的节点
            PushStack(&b,pop_temp->right);//将该节点的右孩子入栈
        }
    }
    (*returnSize)=i;
    return a;
}

​

The specific operation process: (as shown in the figure)

 

Guess you like

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