数据结构笔记--前序、中序和后序遍历的递归与非递归实现

目录

1--前序、中序和后序遍历的递归实现

1-1--前序遍历递归实现

1-2--中序遍历的递归实现

1-3--后序遍历的递归实现

2--前序、中序和后序遍历的非递归实现

2-1--前序遍历的非递归实现

2-2--后序遍历的非递归实现

2-3--中序遍历的非递归实现


1--前序、中序和后序遍历的递归实现

1-1--前序遍历递归实现

前序遍历顺序:根结点、左子树、右子树;

#include 
#include 

struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int value): val(value), left(NULL), right(NULL){};
};

class Solution{
public:
    std::vector preorder(TreeNode *root){
        if(root == NULL) return res;

        res.push_back(root->val); // 根
        preorder(root->left); // 左
        preorder(root->right); // 右
        return res;
    }

    std::vector res;
}; 

int main(int argc, char *argv[]){

    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    TreeNode *Node4 = new TreeNode(4);
    TreeNode *Node5 = new TreeNode(5);
    TreeNode *Node6 = new TreeNode(6);
    TreeNode *Node7 = new TreeNode(7);

    Node1->left = Node2;
    Node1->right = Node3;
    Node2->left = Node4;
    Node2->right = Node5;
    Node3->left = Node6;
    Node3->right = Node7;

    Solution S1;
    std::vector res = S1.preorder(Node1);
    for(int num : res) std::cout << num << " ";

    return 0;
}

1-2--中序遍历的递归实现

中序遍历顺序:左子树、根节点、右子树;

#include 
#include 

struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int value): val(value), left(NULL), right(NULL){};
};

class Solution{
public:
    std::vector inorder(TreeNode *root){
        if(root == NULL) return res;

        inorder(root->left); // 左
        res.push_back(root->val); // 根
        inorder(root->right); // 右
        return res;
    }

    std::vector res;
}; 

int main(int argc, char *argv[]){

    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    TreeNode *Node4 = new TreeNode(4);
    TreeNode *Node5 = new TreeNode(5);
    TreeNode *Node6 = new TreeNode(6);
    TreeNode *Node7 = new TreeNode(7);

    Node1->left = Node2;
    Node1->right = Node3;
    Node2->left = Node4;
    Node2->right = Node5;
    Node3->left = Node6;
    Node3->right = Node7;

    Solution S1;
    std::vector res = S1.inorder(Node1);
    for(int num : res) std::cout << num << " ";

    return 0;
}

1-3--后序遍历的递归实现

后序遍历顺序:左子树、右子树、根节点;

#include 
#include 

struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int value): val(value), left(NULL), right(NULL){};
};

class Solution{
public:
    std::vector postorder(TreeNode *root){
        if(root == NULL) return res;

        postorder(root->left); // 左
        postorder(root->right); // 右
        res.push_back(root->val); // 根
        return res;
    }

    std::vector res;
}; 

int main(int argc, char *argv[]){

    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    TreeNode *Node4 = new TreeNode(4);
    TreeNode *Node5 = new TreeNode(5);
    TreeNode *Node6 = new TreeNode(6);
    TreeNode *Node7 = new TreeNode(7);

    Node1->left = Node2;
    Node1->right = Node3;
    Node2->left = Node4;
    Node2->right = Node5;
    Node3->left = Node6;
    Node3->right = Node7;

    Solution S1;
    std::vector res = S1.postorder(Node1);
    for(int num : res) std::cout << num << " ";

    return 0;
}

2--前序、中序和后序遍历的非递归实现

2-1--前序遍历的非递归实现

流程:初始化根节点入栈;

        ① 结点出栈,处理结点;

        ② 出栈结点的右儿子入栈,出栈结点的左儿子出栈;

        ③ 循环执行第 ① 步;

#include 
#include 
#include 

struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int value): val(value), left(NULL), right(NULL){};
};

class Solution{
public:
    std::vector preorder(TreeNode *root){
        if(root == NULL) return res;
        st.push(root); // 初始化根节点入栈
        while(!st.empty()){
            // 结点出栈
            TreeNode *tmp = st.top();
            st.pop();

            // 处理结点
            res.push_back(tmp->val);

            // 右儿子入栈
            if(tmp->right != NULL) st.push(tmp->right);
            // 左儿子入栈
            if(tmp->left != NULL) st.push(tmp->left);
        }
        return res;
    }

    std::vector res;
    std::stack st;
}; 

int main(int argc, char *argv[]){

    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    TreeNode *Node4 = new TreeNode(4);
    TreeNode *Node5 = new TreeNode(5);
    TreeNode *Node6 = new TreeNode(6);
    TreeNode *Node7 = new TreeNode(7);

    Node1->left = Node2;
    Node1->right = Node3;
    Node2->left = Node4;
    Node2->right = Node5;
    Node3->left = Node6;
    Node3->right = Node7;

    Solution S1;
    std::vector res = S1.preorder(Node1);
    for(int num : res) std::cout << num << " ";

    return 0;
}

2-2--后序遍历的非递归实现

流程:初始化两个栈,根节点入其中一个栈1,另一个栈2用于收集;

        ① 栈1的结点出栈;

        ② 出栈结点入收集栈2;

        ③ 出栈结点的左儿子入栈1,出栈结点的右儿子入栈2;

        ④ 重复第①步,直至栈1空;

        ⑤ 收集栈的结点依次出栈,此时顺序为后序遍历;

#include 
#include 
#include 

struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int value): val(value), left(NULL), right(NULL){};
};

class Solution{
public:
    std::vector postorder(TreeNode *root){
        if(root == NULL) return res;
        st1.push(root); // 初始化根节点入栈1
        while(!st1.empty()){
            // 结点出栈
            TreeNode *tmp = st1.top();
            st1.pop();

            // 出栈结点入收集栈2
           st2.push(tmp);

            // 左儿子入栈1
            if(tmp->left != NULL) st1.push(tmp->left);
            // 右儿子入栈1
            if(tmp->right != NULL) st1.push(tmp->right);
        }
        // 遍历收集栈2
        while(!st2.empty()){
            res.push_back(st2.top()->val);
            st2.pop();
        }
        return res;
    }

    std::vector res;
    std::stack st1;
    std::stack st2;
}; 

int main(int argc, char *argv[]){

    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    TreeNode *Node4 = new TreeNode(4);
    TreeNode *Node5 = new TreeNode(5);
    TreeNode *Node6 = new TreeNode(6);
    TreeNode *Node7 = new TreeNode(7);

    Node1->left = Node2;
    Node1->right = Node3;
    Node2->left = Node4;
    Node2->right = Node5;
    Node3->left = Node6;
    Node3->right = Node7;

    Solution S1;
    std::vector res = S1.preorder(Node1);
    for(int num : res) std::cout << num << " ";

    return 0;
}

2-3--中序遍历的非递归实现

流程:初始化一个栈,根节点入栈;

        ①:左子结点全部入栈;

        ②:结点出栈,处理结点;

        ③:对出栈结点的右子树执行第 ① 步操作;

#include 
#include 
#include 

struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int value): val(value), left(NULL), right(NULL){};
};

class Solution{
public:
    std::vector inorder(TreeNode *root){
        if(root == NULL) return res;
        while(!st.empty() || root != NULL){
            if(root != NULL){ // 左子结点全部入栈
                st.push(root);
                root = root->left;
            }
            else{
                TreeNode *tmp = st.top(); // 结点出栈
                st.pop(); 
                res.push_back(tmp->val);
                root = tmp->right; // 对出栈结点执行左子结点全部入栈的操作,即回到while循环判断
            }
        }
        return res;
    }

    std::vector res;
    std::stack st;
}; 

int main(int argc, char *argv[]){

    TreeNode *Node1 = new TreeNode(1);
    TreeNode *Node2 = new TreeNode(2);
    TreeNode *Node3 = new TreeNode(3);
    TreeNode *Node4 = new TreeNode(4);
    TreeNode *Node5 = new TreeNode(5);
    TreeNode *Node6 = new TreeNode(6);
    TreeNode *Node7 = new TreeNode(7);

    Node1->left = Node2;
    Node1->right = Node3;
    Node2->left = Node4;
    Node2->right = Node5;
    Node3->left = Node6;
    Node3->right = Node7;

    Solution S1;
    std::vector res = S1.inorder(Node1);
    for(int num : res) std::cout << num << " ";

    return 0;
}

你可能感兴趣的:(数据结构,算法)