二叉树的遍历

1、二叉树的遍历

(1)前序遍历

        1)递归方法

void pre(BinTree* root){
    if(root == nullptr) return;
    cout << root->val << ',';
    pre(root->left);
    pre(root->right);
}

       2)非递归方法:先将右孩子压栈,再将左孩子压栈

void pre_nore(BinTree* root){
    if(root){
        stack<BinTree*> s;
        s.push(root);
        while(!s.empty()){
            cout << s.top()->val << ',';
            root = s.top();
            s.pop();
            if(root->right != nullptr){
                s.push(root->right);
            }
            if(root->left != nullptr){
                s.push(root->left);
            }
        }
    }
}

(2)中序遍历

        1)递归方法

void middle(BinTree* root){
    if(root == nullptr) return;
    middle(root->left);
    cout << root->val << ',';
    middle(root->right);
}

        2)非递归的方法

void middle_nore(BinTree* root){
    if(root){
        stack<BinTree*> s;
        while(!s.empty() || root != nullptr){
            if(root != nullptr){
                s.push(root);
                root = root->left;
            }
            else{
                cout << s.top()->val << ',';
                root = s.top()->right;
                s.pop();
            }
        }
    }
}

(3)后序遍历

        1)递归方法

void back(BinTree* root){
    if(root == nullptr) return;
    back(root->left);
    back(root->right);
    cout << root->val << ',';    
}

        2)非递归方法1:两个栈

void back_nore1(BinTree* root){
    if(root){
        stack<BinTree*> s1;
        stack<int> s2;
        s1.push(root);
        while (!s1.empty())
        {
            root = s1.top();
            s2.push(root->val);
            s1.pop();
            if(root->left != nullptr){
                s1.push(root->left);
            }
            if(root->right != nullptr){
                s1.push(root->right);
            }
        }

        while(!s2.empty()){
            cout<< s2.top()<<',';
            s2.pop();
        }
        
    }
}

        3)非递归的方法2

void back_nore2(BinTree* root){
    if(root){
        stack<BinTree*> s;
        BinTree* c; //指向栈顶元素
        BinTree* h = root; //指向刚被弹出的位置
        s.push(root);
        while (!s.empty())
        {
            c = s.top();
            if(c->left != nullptr && h != c->left && h != c->right){
                s.push(c->left);
            }
            else if(c->right != nullptr && h != c->right){
                s.push(c->right);
            }
            else{
                cout << c->val << ',';
                h = c;
                s.pop();
            }
        }
        
    }
}

2、二叉树的层序遍历

void level_traverse(BinTree* root){
    if(root){
        queue<BinTree*> Q;
        BinTree* CurEnd = root;
        BinTree* nextEnd = nullptr;
        Q.push(root);
        while(!Q.empty()){
            BinTree* curNode = Q.front();
            cout << curNode->val << ',';
            Q.pop();
            if(curNode->left != nullptr){
                Q.push(curNode->left);
                nextEnd = curNode->left;
            }

            if(curNode->right != nullptr){
                Q.push(curNode->right);
                nextEnd = curNode->right;
            }

            if(curNode == CurEnd){
                CurEnd = nextEnd;
                cout << endl;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值