二叉树的前中后序以及层次遍历整理

144. 二叉树的前序遍历

 1.递归

class Solution {
public:
    vector<int> vc;
    vector<int> preorderTraversal(TreeNode* root) {
        vc.clear();
        if(root == nullptr) return vc;
        dfs(root);
        return vc;
    }
    void dfs(TreeNode* root){
        if(root == nullptr) return;
        vc.push_back(root->val);
        dfs(root->left);
        dfs(root->right);
    }
    
};

2.迭代

class Solution {
public:
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> vc;
        if(root  == nullptr) return vc;
        stack<TreeNode* > sta;
        sta.push(root);
        while(!sta.empty()){
            TreeNode* node = sta.top();
            sta.pop();
            // if(node == nullptr) continue;
            vc.push_back(node->val);
            if(node->right != nullptr) sta.push(node->right);
            if(node->left != nullptr) sta.push(node->left);
        }
        return vc;
    }
};

94. 二叉树的中序遍历

 1.递归

class Solution {
public:
    vector<int> vc;
    vector<int> inorderTraversal(TreeNode* root) {
        if(root == nullptr) return vc;
        dfs(root);
        return vc;
    }
    void dfs(TreeNode* root){
        if(root == nullptr) return;
        dfs(root -> left);
        vc.push_back(root->val);
        dfs(root -> right);
    }
    
};

2.迭代

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> vc;
        if(root == nullptr) return vc;
        stack<TreeNode* > sta;
        // sta.push(root);
        while(root || !sta.empty()){
           while(root){
               sta.push(root);
               root = root -> left;
           }
           root = sta.top();
           sta.pop();
           vc.push_back(root->val);
           root = root->right;
        }
        return vc;
    }
};

145. 二叉树的后序遍历

 1.递归

class Solution {
public:
    vector<int> vc;
    vector<int> postorderTraversal(TreeNode* root) {
        vc.clear();
        if(!root) return vc;
        dfs(root);
        return vc;
    }
    void dfs(TreeNode* root){
        if(!root) return ;
        dfs(root->left);
        dfs(root->right);
        vc.push_back(root->val);
    }
};

2.迭代


class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> vc;
        if(!root) return vc;
        stack<TreeNode* > sta;
        TreeNode* last;
        while(root || !sta.empty()){
            while(root){
                sta.push(root);
                root = root->left;
            }
            root = sta.top();
            if(root->right == nullptr || root->right == last){
                sta.pop();
                last = root;
                vc.push_back(root->val);
                root = nullptr;
            }else {
                root = root->right;
            }
        }
        return vc;
    }
};

102. 二叉树的层序遍历

难度中等1230

给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> vc;
        if(!root) return vc;
        queue<TreeNode*> ac;
        vc.clear(); 
        ac.push(root);
        while(!ac.empty()){
            int n = ac.size();
            vector<int> x;
            for(int i = 0; i < n; i++){
                auto b = ac.front();
                x.push_back(b->val);
                ac.pop();
                if(b->left) ac.push(b->left);
                if(b->right) ac.push(b->right);
            }
            vc.push_back(x);
        }
        return vc;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值