二叉树:层次遍历变形题目汇总

107. 二叉树的层序遍历 II
将层序遍历数组paths反转下即可

/**
 * 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>> paths;
        if(root == NULL)
        {
    
    
            return paths;
        }
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty())
        {
    
    
            int size = q.size();
            vector<int> path;
            while(size--)
            {
    
    
                TreeNode *node = q.front();
                q.pop();
                path.push_back(node->val);
                if(node->left != NULL)
                {
    
    
                    q.push(node->left);
                }
                if(node->right != NULL)
                {
    
    
                    q.push(node->right);
                }
            }
            paths.push_back(path);
        }
        reverse(paths.begin(), paths.end());
        return paths;
    }
};

199.二叉树的右视图
将层序遍历每层最后一个数字放入paths数组中,价格判断即可

/**
 * 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> rightSideView(TreeNode* root) {
    
    
        vector<int> paths;
        if(root == NULL)
        {
    
    
            return paths;
        }
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty())
        {
    
    
            int size = q.size();
            while(size--)
            {
    
    
                TreeNode *node = q.front();
                q.pop();
                if(size == 0)//每层最后一个节点放入数组中
                {
    
    
                    paths.push_back(node->val);
                }
                if(node->left != NULL)
                {
    
    
                    q.push(node->left);
                }
                if(node->right != NULL)
                {
    
    
                    q.push(node->right);
                }
            }
        }
        return paths;
    }
};

429. N 叉树的层序遍历
这道题依旧是模板题,只不过一个节点有多个孩子了

/*
// 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>> paths;
        if(root == NULL)
        {
    
    
            return paths;
        }
        queue<Node*> q;
        q.push(root);
        while(!q.empty())
        {
    
    
            int size = q.size();
            vector<int> path;
            while(size--)
            {
    
    
                Node *node = q.front();
                q.pop();
                path.push_back(node->val);
                for (int i = 0; i < node->children.size(); i++) 
                {
    
     // 将节点孩子加入队列
                    if (node->children[i]) 
                    q.push(node->children[i]);
                }
            }
            paths.push_back(path);
        }
        return paths;
    }
};

总结

二叉树的层序遍历,就是图论中的广度优先搜索在二叉树中的应用,需要借助队列来实现(此时是不是又发现队列的应用了)。

学会二叉树的层序遍历,可以一口气撸完leetcode上五道题目:

  • 102.二叉树的层序遍历
  • 107.二叉树的层次遍历II
  • 199.二叉树的右视图
  • 637.二叉树的层平均值
  • 589.N叉树的前序遍历

猜你喜欢

转载自blog.csdn.net/cckluv/article/details/112748500
今日推荐