BFS 树的层次遍历

团灭leetcode 树的层次遍历问题,层次遍历本质就是BFS广度优先搜索,在前文https://blog.csdn.net/weixin_43976833/article/details/105740752的基础上稍加改动即可完成。
leetcode 102. 二叉树的层序遍历
题目:https://leetcode-cn.com/problems/binary-tree-level-order-traversal/

/**
 * 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 == NULL) return res;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()) {
            vector<int> curr_level;
            int nums = q.size();
            for(int i = 0; i < nums; i++) {
                TreeNode* curr_node = q.front();
                q.pop();
                curr_level.push_back(curr_node->val);
                if(curr_node->left != NULL) 
                    q.push(curr_node->left);
                if(curr_node->right != NULL) 
                    q.push(curr_node->right);
            }
            res.push_back(curr_level);
        }
        return res;
    }
};

leetcode 107. 二叉树的层序遍历2
题目:https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/
核心:在lc 102的基础上,改用 insert 从头插入

class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> res;
        if(root == NULL) return res;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty()) {
            vector<int> curr_level;
            int nums = q.size();
            for(int i = 0; i < nums; i++) {
                TreeNode* curr_node = q.front();
                q.pop();
                curr_level.push_back(curr_node->val);
                if(curr_node->left != NULL) 
                    q.push(curr_node->left);
                if(curr_node->right != NULL) 
                    q.push(curr_node->right);
            }
            res.insert(res.begin(), curr_level);
        }
        return res;
    }
};

leetcode 103. 二叉树的锯齿形层次遍历
题目:https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/
核心:在lc 102的基础上,(1) 用 insert 从头插入; (2) 使用标志位.

class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> res;
        if(root == NULL) return res;
        queue<TreeNode*> q;
        q.push(root);
        bool inverse = false;
        while(!q.empty()) {
            vector<int> curr_level;
            int nums = q.size();
            for(int i = 0; i < nums; i++) {
                TreeNode* curr_node = q.front();
                q.pop();
                if(!inverse)
                    curr_level.push_back(curr_node->val);
                else if(inverse)
                    curr_level.insert(curr_level.begin(), curr_node->val);
                if(curr_node->left != NULL) 
                    q.push(curr_node->left);
                if(curr_node->right != NULL) 
                    q.push(curr_node->right);
            }
            inverse = !inverse;
            res.push_back(curr_level);
        }
        return res;        
    }
};

leetcode 429. N叉树的层序遍历
题目:https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/
核心:本质与lc 102相同

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        vector<vector<int>> res;
        if(root == NULL) return res;
        queue<Node*> q;
        q.push(root);
        while(!q.empty()) {
            vector<int> curr_level;
            int nums = q.size();
            for(int i = 0; i < nums; i++) {
                Node* curr_node = q.front();
                q.pop();
                curr_level.push_back(curr_node->val);
                for(int i = 0; i < curr_node->children.size(); i++) {
                    q.push(curr_node->children[i]);
                }
            }
            res.push_back(curr_level);
        }
        return res;        
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值