美文网首页
二叉树:前序/中序/后序/层序遍历 (递归&非递归 c++版本)

二叉树:前序/中序/后序/层序遍历 (递归&非递归 c++版本)

作者: 洛丽塔的云裳 | 来源:发表于2020-04-17 21:49 被阅读0次

    参考 https://www.cnblogs.com/bigsai/p/11393609.html

    1. 前序遍历

    前序的规则就是根结点 ---> 左子树 ---> 右子树.我们在调用递归前进行节点操作。对于前序,就是先访问(输出)该节点。而递归左,递归右侧,会优先递归左侧。直到没有左节点。才会停止。访问次序大致为:

    测试用例:


    (1) 递归版本

    struct TreeNode {
       int val;
       TreeNode *left;
       TreeNode *right;
       TreeNode(int x): val(x), left(NULL), right(NULL) {}
    };
    void preOrder(TreeNode *root) {
        if (root) {
            std::cout << root->val << ",";
            preOrder(root->left);
            preOrder(root->right);
        }
    }
    // 以下为测试例子
    int main() {
        TreeNode *root1 = new TreeNode(3);
        TreeNode *left1 = new TreeNode(9);
        TreeNode *right1 = new TreeNode(20);
        TreeNode *left2 = new TreeNode(15);
        TreeNode *right2 = new TreeNode(7);
        root1 -> left = left1;
        root1 -> right = right1;
        right1 -> left = left2;
        right1 -> right = right2;
        preOrder(root1);
    }
    

    前序遍历结果:3,9,20,15,7

    (2) 非递归版本

    利用栈,栈的顺序为先进先出。那么顺序如何添加?递归是左递归,右递归。但是利用栈要相反,因为如果左进栈、右进栈会出现以下后果:

    所以,利用递归的思路,需要先放右节点进栈,再放左节点进栈,这个下次·再取节点取到左节点·,这个节点再右节点进栈,左节点进栈。然后循环一直到最后会一直优先取到左节点。达到和递归顺序相仿效果。
    void preOrder(TreeNode *root) {
        stack<TreeNode *> s;
        s.push(root);
        while (!s.empty()) {
            TreeNode *curr = s.top();
            std::cout << curr->val << ",";
            s.pop();
            if (curr -> right)
                s.push(curr -> right);
            if (curr -> left)
                s.push(curr -> left);
        }
    }
    

    2. 中序遍历

    中序遍历的规则是:左子树---> 根结点 ---> 右子树。所以我们访问节点的顺序需要变

    (1) 递归版本

    struct TreeNode {
       int val;
       TreeNode *left;
       TreeNode *right;
       TreeNode(int x): val(x), left(NULL), right(NULL) {}
    };
    
    void midOrder(TreeNode *root) {
        if (root) {
            midOrder(root->left);
            std::cout << root->val << ",";
            midOrder(root->right);
        }
    }
    int main() {
        TreeNode *root1 = new TreeNode(3);
        TreeNode *left1 = new TreeNode(9);
        TreeNode *right1 = new TreeNode(20);
        TreeNode *left2 = new TreeNode(15);
        TreeNode *right2 = new TreeNode(7);
        root1 -> left = left1;
        root1 -> right = right1;
        right1 -> left = left2;
        right1 -> right = right2;
        midOrder(root1);
    }
    

    中序遍历结果:9,3,15,20,7

    (2) 非递归版本

    中序排列的顺序是: 左节点,根节点,右节点。那么我们在经过根节点的前面节点不能释放, 因为后面还需要用到它。所以要用栈先储存。
    它的规则大致为:

    • 栈依次存入左节点所有点,直到最左侧在栈顶。
    • 开始抛出栈顶并访问。(例如第一个抛出2)。如果有右节点。那么将右节点加入栈中,然后右节点一致左下遍历直到尾部。


    void midOrder2(TreeNode *root) {
        stack<TreeNode *> s;
        TreeNode *p = root;
        while (!s.empty() || p) {
           //一直遍历到左子树最下边,边遍历边保存根节点到栈中
           while(p) {
               s.push(p);
               p = p->left;
           }
           // 当p为空时,说明已经到达左子树最下边,这时需要出栈了
           if (!s.empty()) {
             p = s.top();
             std::cout << p->val << ",";
             s.pop();
             p = p->right;
           }
        }
    }
    

    3. 后序遍历

    后序遍历就是左子树 ---> 右子树 ---> 根结点

    (1) 递归版本

    struct TreeNode {
       int val;
       TreeNode *left;
       TreeNode *right;
       TreeNode(int x): val(x), left(NULL), right(NULL) {}
    };
    
    void postOrder(TreeNode *root) {
        if (root) {
            postOrder(root->left);
            postOrder(root->right);
            std::cout << root->val << ",";
        }
    }
    
    int main() {
        TreeNode *root1 = new TreeNode(3);
        TreeNode *left1 = new TreeNode(9);
        TreeNode *right1 = new TreeNode(20);
        TreeNode *left2 = new TreeNode(15);
        TreeNode *right2 = new TreeNode(7);
        root1 -> left = left1;
        root1 -> right = right1;
        right1 -> left = left2;
        right1 -> right = right2;
        postOrder(root1);
    }
    

    后序遍历结果:9,15,7,20,3

    (2) 非递归版本

    void postOrder2(TreeNode *root) {
        stack<pair<TreeNode*, bool> > s;
        TreeNode *p = root;
        while (!s.empty() || p) {
                while (p) {
                    s.push(make_pair(p, false));
                    p = p->left;
                }
                if (!s.empty()) {
                    pair<TreeNode*, bool> curr = s.top();
                    if (curr.second) {
                         std::cout << curr.first->val <<  ",";
                         s.pop();
                    }
                    else {
                        curr.second = true;
                        p = p->right;
                    }
                }
        }
    }
    

    4. 层序遍历

    https://blog.csdn.net/FX677588/article/details/74276513
    层序遍历所要解决的问题很好理解,就是按二叉树从上到下,从左到右依次打印每个节点中存储的数据。如下图:


    按层序遍历的原则,打印顺序依次应该是:A->B->C->D->E->F->G
    void levelOrder(TreeNode *root){
        queue<TreeNode *> fatherQueue, childQueue;
        TreeNode *p = root;
        if (p)
            fatherQueue.push(p);
        while (!fatherQueue.empty()) {
            p = fatherQueue.front();
            std::cout << p->val << ",";
            fatherQueue.pop();
            if (p->left)
                childQueue.push(p->left);
            if (p->right)
                childQueue.push(p->right);
            if (fatherQueue.empty() && !childQueue.empty()) {
                swap(fatherQueue, childQueue);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:二叉树:前序/中序/后序/层序遍历 (递归&非递归 c++版本)

          本文链接:https://www.haomeiwen.com/subject/dactvhtx.html