二叉树 | 二叉树的层次遍历

问题:二叉树的层次遍历

问题链接
在这里插入图片描述
在这里插入图片描述

解题思路

使用队列和两个统计数量的变量cnt1、cnt2。cnt1表示本层节点的数量,初始为1,即根节点。cnt2表示下一层节点的数量。

C++代码

/**
 * 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>> ans;
        if(!root) return ans;
        int cnt1 = 1, cnt2 = 0;//本层节点和下层节点的数量
        queue<TreeNode*>q;//队列
        q.push(root);//压入根节点
        while(!q.empty()){
            vector<int>v; 
            //将本层的cnt1个节点出队列,并统计下一层的节点数量
            while(cnt1--){
                TreeNode *tmp = q.front();
                q.pop();
                v.push_back(tmp->val);
                if(tmp->left){
                    q.push(tmp->left);//加入左孩子
                    cnt2++;//下一层节点数加一
                }
                if(tmp->right){
                    q.push(tmp->right);//加入右孩子
                    cnt2++;//下一层节点数加一
                }
            }
            ans.push_back(v);//将本层的统计记过放入结果ans中
            cnt1 = cnt2;//更新cnt1
            cnt2 = 0;//重置cnt2
        }
        return ans;
    }
};

问题:二叉树的层次遍历 II

问题链接在这里插入图片描述
在这里插入图片描述

解题思路

和 “二叉树的层次遍历”一题差不多,只是将讲过反转一下即可。

C++代码

/**
 * 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>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> ans;
        if(!root) return ans;
        int cnt1 = 1, cnt2 = 0;//本层节点和下层节点的数量
        queue<TreeNode*>q;//队列
        q.push(root);//压入根节点
        while(!q.empty()){
            vector<int>v; 
            //将本层的cnt1个节点出队列,并统计下一层的节点数量
            while(cnt1--){
                TreeNode *tmp = q.front();
                q.pop();
                v.push_back(tmp->val);
                if(tmp->left){
                    q.push(tmp->left);//加入左孩子
                    cnt2++;//下一层节点数加一
                }
                if(tmp->right){
                    q.push(tmp->right);//加入右孩子
                    cnt2++;//下一层节点数加一
                }
            }
            ans.push_back(v);//将本层的统计记过放入结果ans中
            cnt1 = cnt2;//更新cnt1
            cnt2 = 0;//重置cnt2
        }
        //将结果反转一下
        int i = 0, j = ans.size()-1;
        while(i < j){
            vector<int> tmp = ans[i];
            ans[i] = ans[j];
            ans[j] =tmp;
            i++, j--;
        }
        return ans;
    }
};

问题:二叉树的锯齿形层次遍历

问题链接
在这里插入图片描述

解题思路

二叉树的层次遍历得到的结果向量进行部分元素的反转即可

C++代码

/**
 * 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>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> ans;
        if(!root) return ans;
        int cnt1 = 1, cnt2 = 0;//本层节点和下层节点的数量
        queue<TreeNode*>q;//队列
        q.push(root);//压入根节点
        while(!q.empty()){
            vector<int>v; 
            //将本层的cnt1个节点出队列,并统计下一层的节点数量
            while(cnt1--){
                TreeNode *tmp = q.front();
                q.pop();
                v.push_back(tmp->val);
                if(tmp->left){
                    q.push(tmp->left);//加入左孩子
                    cnt2++;//下一层节点数加一
                }
                if(tmp->right){
                    q.push(tmp->right);//加入右孩子
                    cnt2++;//下一层节点数加一
                }
            }
            ans.push_back(v);//将本层的统计记过放入结果ans中
            cnt1 = cnt2;//更新cnt1
            cnt2 = 0;//重置cnt2
        }
        //进行反转操作:对下标是奇数的链表进行反转
        for(int i = 1; i < ans.size(); i += 2) reverse(ans[i].begin(), ans[i].end());
        return ans;
    }
};
发布了860 篇原创文章 · 获赞 270 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/SongBai1997/article/details/104713323