牛客网 树问题 C++实现

1:分别用递归和非递归方式实现二叉树的前序、中序和后序遍历

class Solution{
public:
    //递归方式依次实现二叉树前序、中序、后序遍历
    void preOrder(TreeNode *root,vector<int>&vector){
        if(root!=NULL){
            vector.emplace_back(root->val);
            preOrder(root->left,vector);
            preOrder(root->right,vector);
        }
    }
    void inOrder(TreeNode *root,vector<int>&vector){
        if(root!=NULL){
            inOrder(root->left,vector);
            vector.emplace_back(root->val);
            inOrder(root->right,vector);

        }
    }
    void postOrder(TreeNode *root,vector<int>&vector){
        if(root!=NULL){
            postOrder(root->left,vector);
            postOrder(root->right,vector);
            vector.emplace_back(root->val);
        }
    }

    //非递归方式实现先序、中序、后序遍历
    void preOrderUnRecur(TreeNode *root,vector<int>&vector){
        if(root==NULL)return;
        stack<TreeNode*>stack;
        stack.emplace(root);
        while (!stack.empty()){
            TreeNode *node=stack.top();
            stack.pop();
            vector.emplace_back(node->val);
            //按照右左的顺序压入节点,这样弹出时就是根->左->右
            if(node->right!=NULL)
                stack.push(node->right);
            if(node->left!=NULL)
                stack.push(node->left);
        }
    }

    //利用栈实现非递归中序遍历
    void inOrderUnRecur(TreeNode *root,vector<int>&vector){
        if(root==NULL)return;
        stack<TreeNode*>stack;
        while (!stack.empty()||root!=NULL){
            if(root!=NULL){//栈空
                stack.push(root);
                root=root->left;//将左子树压入栈,只有root为NULL才取遍历右子树,左中右
            }
            else{
               root=stack.top();
               stack.pop();
               vector.emplace_back(root->val);
               root=root->right;
            }
        }

    }

    //利用两个栈实现后序非递归遍历
    void postOrderUnRecur1(TreeNode *root,vector<int>&vector){
        if(root==NULL)return;
        stack<TreeNode*>A;
        stack<TreeNode*>B;
        A.push(root);//加入A中
        while (!A.empty()){
            root=A.top();
            A.pop();
            B.push(root);//A中节点依次弹出放入B中
            if(root->left!=NULL)
                A.push(root->left);
            if(root->right!=NULL)
                A.push(root->right);
        }
        while (!B.empty()){
            vector.push_back(B.top()->val);
            B.pop();
        }


    }
    //利用单个栈实现二叉树后序遍历
    void postOrderUnRecur2(TreeNode *root,vector<int>&vector){
        if(root==NULL)return;
        stack<TreeNode*>stack;
        stack.push(root);
        TreeNode *temp=NULL;
        while (!stack.empty()){
            temp=stack.top();
            if(temp->left!=NULL&&root!=temp->left&&root!=temp->right)
                stack.push(temp->left);
            else if(temp->right!=NULL&&root!=temp->right)
                stack.push(temp->right);
            else{
                vector.push_back(stack.top()->val);
                stack.pop();
                root=temp;
            }
        }
    }
};

2:较为直观地打印二叉树

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值