二叉树遍历(递归前中后+非递归前中后+层序)

1、二叉树递归遍历

1.1、先序(根左右)

链接

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    void preOrder(TreeNode*  root,vector<int> & res){
        if(!root) return;
        res.push_back(root->val);
        preOrder(root->left,res);
        preOrder(root->right,res);
    }
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int>res;
        preOrder(root,res);
        return res;
    }
};

//  1.2、中序(左根右)

class Solution {
public:
    void inOrder(TreeNode * root,vector<int>& res)  // 递归函数
    {
        if(!root)  // 如果此节点为空,返回
        return;
        inOrder(root->left,res);  // 递归左节点,
        res.push_back(root->val);   // 把当前值写入结果集
        inOrder(root->right,res);  //递归右节点
    }
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> res;  // 初始化结果集
        inOrder(root,res);  // 递归函数
        return res;       
    }
};

// 1.3、后序(左右根)

class Solution {
public:
    void postOrder(TreeNode * root,vector<int>& res){
        if(!root) return ;
        postOrder(root->left,res);
        postOrder(root->right,res);
        res.push_back(root->val);
    }
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int>res;
        postOrder(root,res);
        return res;
    }
};

2、非递归遍历(前序根左右,后序左右根)


//  2.1、前序(根左右)

class Solution {
public:    
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int>res;
        if(!root) return res;
        stack<TreeNode*>st;
        st.push(root);

        while(!st.empty()){
            auto tem = st.top();
            st.pop();
            res.push_back(tem->val);
            if(tem->right) st.push(tem->right);  // 这里不要写错:(!tem->right)
            if(tem->left) st.push(tem->left);
        }
       return res;
    }


// 2.3、后序(左右根)

class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int>res;
        if(!root) return res;
        stack<TreeNode*>st;
        st.push(root);

        while(!st.empty()){
            auto tem = st.top();
            st.pop();
            res.push_back(tem->val);
            if(tem->left) st.push(tem->left);
            if(tem->right) st.push(tem->right);
        }
        reverse(res.begin(),res.end());
        return res;
    }
};

2.4、层序非递归遍历二叉树

使用队列结构,
题目链接

 auto tem = que.front();
 que.pop();
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> levelOrder(TreeNode* root) {
        vector<int>res;
        if(!root) return res;
        queue<TreeNode*>que;
        que.push(root);

        while(!que.empty()){
            auto tem = que.front();
            que.pop();
            res.push_back(tem->val);
            if(tem->left) que.push(tem->left);
            if(tem->right) que.push(tem->right);
        }
        return res;
    }
};

2.4.4 从下往上打印:

使用一下vector的逆序函数
reverse(res.begin(),res.end());

2.5、之字形打印二叉树

题目链接
使用两层vector

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        if(!root) return res;  // 特判
        int flag = 0;
        queue<TreeNode*>que;
        que.push(root);

        while(!que.empty()){
            int n = que.size();
            vector<int>nums;   // 局部nums所以不用在最后重置为0
            while(n--){
                auto tem = que.front();
                que.pop();
                nums.push_back(tem->val);
                if(tem->left) que.push(tem->left);
                if(tem->right) que.push(tem->right);
            }
            if(flag % 2 == 1){
                reverse(nums.begin(),nums.end());
            }
            res.push_back(nums);
            flag++;
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值