Binary tree preorder, preorder, postorder traversal, traverse the level (recursive and non-recursive)

Binary tree preorder, preorder, postorder traversal, traverse the level (recursive and non-recursive)

Preorder

Preorder traversal of the binary tree is traversed to the root, followed by the left subtree, then this order is right subtree.

void preorder(BiTree T){
    if(T!=NULL){
        visit(T);
        preorder(T->left);
        preorder(T->right);
    }
}

Non-recursive algorithm is as follows

struct Command{
    string s;
    TreeNode* node;
    Command(string s,TreeNode* node):s(s),node(node){}
};
class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> res;
        if(root == NULL) return res;
        
        stack<Command> stack;
        stack.push(Command("go",root));
        while(!stack.empty()){
            Command command = stack.top();
            stack.pop();
            if(command.s == "print")
                res.push_back(command.node->val);
            else{
                //assert(command == "go");
                if(command.node->right)
                    stack.push(Command("go",command.node->right));
                if(command.node->left)
                    stack.push(Command("go",command.node->left));
                stack.push(Command("print",command.node));
            }
        }
        return res;
    }
};

Preorder

In traversing Binary Tree is the first to traverse the left subtree, then visit the root, then traverse the right subtree.

void inorder(BiTree T){
    if(T!=NULL){
        inorder(T->left);
        visit(T);
        inorder(T->right);
    }
}

With the stack, recursive algorithm to convert binary tree is a non-recursive algorithm. To access all of the root node and left them one by one into the stack. Then a Stack node p, p at this time is not left child node or the left child nodes have already visited, p access, and then scans the right child node, which is pushed onto the stack and then scans the right child node All left node and 11 into the stack. Until the stack is empty.

void inorder2(BiTree T){
    InitStack(S);
    BiTree p=T;
    while(p||!IsEmpty(S)){
        if(p){
            push(S,p);
            p=p->left;
		}
        else{
            pop(S,p);visit(p);
            p=p->right;
		}
    }
}

This is a conventional non-recursive textbook traversing algorithm, given a common analog of a process stack below the stack to the non-recursive algorithm is a recursive algorithm into the process stack.

struct Command{
    string s;
    TreeNode* node;
    Command(string s,TreeNode* node):s(s),node(node){}
};
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;
        if(root == NULL) return res;
        
        stack<Command> stack;
        stack.push(Command("go",root));
        while(!stack.empty()){
            Command command = stack.top();
            stack.pop();
            if(command.s == "print")
                res.push_back(command.node->val);
            else{
                //assert(command == "go");
                if(command.node->right)
                    stack.push(Command("go",command.node->right));
                stack.push(Command("print",command.node));
                if(command.node->left)
                    stack.push(Command("go",command.node->left));
            }
        }
        return res;
    }
};

Postorder

After traversing Binary Tree is to visit the left subtree, then visit the right subtree, then visit the root node.

void postorder(BiTree T){
    if(T!=NULL){
        postorder(T->left);
        postorder(T->right);
        visit(T);
    }
}

Non-recursive binary tree traversal sequence after sequence traversal is to visit the left subtree, then visit the right subtree, last accessed the root node. When the stack to a storage node, the root node must distinguish return, the left subtree is returned, or right subtree is returned. You need to use auxiliary pointer r, pointing to recently visited node. Or another node to add a flag field, whether the record has been accessed.

void postorder2(BiTree T){
    InitStack(S);
    p=T;
    r=NULL;
    while(p||!IsEmpty(S)){
        if(p){					//走到最左边
            push(S,p);
            p=p->left;
        }
        else{
            GetTop(S,p);
            if(p->right&&r->left!=r){
                p=p->right;
                push(S,p);
                p-p->left;
            }
            else{
                pop(S,p);
                visit(p->data);
                r=p;
                p=NULL;
            }
        }
    }
}
struct Command{
    string s;
    TreeNode* node;
    Command(string s,TreeNode* node):s(s),node(node){}
};
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> res;
        if(root == NULL) return res;
        
        stack<Command> stack;
        stack.push(Command("go",root));
        while(!stack.empty()){
            Command command = stack.top();
            stack.pop();
            if(command.s == "print")
                res.push_back(command.node->val);
            else{
                //assert(command == "go");
                stack.push(Command("print",command.node));
                if(command.node->right)
                    stack.push(Command("go",command.node->right));
                if(command.node->left)
                    stack.push(Command("go",command.node->left));
            }
        }
        return res;
    }
};

Traverse the level

Here Insert Picture Description
Traverse the level required by means of a queue, binary tree root into the first team, then the team, access the node if it has left subtree, then the root node left child into the team, if it has the right subtree, then Right subtree root node into the team. Then dequeued to the team access node, until the queue is empty.

void levelorder(BiTree T){
    InitQueue(Q);
    BiTree p;
    EnQueue(Q,T);
    while(!IsEmpty(Q)){
        DeQueue(Q,p);
        visit(p);
        if(p->left!=NULL)
            EnQueue(Q,p->left);
        if(p->right!=NULL)
            EnQueue(Q,p->right);
    }
    
}
Published 28 original articles · won praise 34 · views 2674

Guess you like

Origin blog.csdn.net/weixin_44289697/article/details/104490912