• 429. N-ary Tree Level Order Traversal


    Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

    For example, given a 3-ary tree:

    We should return its level order traversal:

    [
         [1],
         [3,2,4],
         [5,6]
    ]
    

    Note:

    1. The depth of the tree is at most 1000.
    2. The total number of nodes is at most 5000.

    前序遍历时记录节点位于那一层。

    #include<vector>
    #include <cstdlib>
    #include<iostream>
    #include <unordered_set>
    #include <algorithm>
    #include<string>
    
    using namespace std;
    
    // Definition for a Node.
    class Node {
    public:
        int val = NULL;
        vector<Node *> children;
    
        Node() {}
    
        Node(int _val, vector<Node *> _children) {
            val = _val;
            children = _children;
        }
    };
    
    class Solution {
    public:
        vector<vector<int>> levelOrder(Node *root) {
            vector<vector<int>> res;
            preorder(root, res, 0);
            return res;
        }
    
        void preorder(Node *root, vector<vector<int>> &res, int level) {
            if (root == NULL) return;
            if (res.size() < level + 1)
                res.push_back({});
            res[level].push_back(root->val);
            for (int i = 0; i < root->children.size(); ++i) {
                preorder(root->children[i], res, level + 1);
            }
        }
    };
    
    
    int main() {
        Node *node5 = new Node(5, {});
        Node *node6 = new Node(6, {});
        Node *node3 = new Node(3, {node5, node6});
        Node *node2 = new Node(2, {});
        Node *node4 = new Node(4, {});
        Node *node1 = new Node(1, {node3, node2, node4});
        Solution solution;
        vector<vector<int>> res = solution.levelOrder(node1);
        for (int i = 0; i < res.size(); ++i) {
            for (int j = 0; j < res[i].size(); ++j)
                cout << res[i][j] << " ";
            cout << endl;
        }
        return 0;
    }

    效率比较低,只超过了3.97的提交
    Your runtime beats 3.97 % of cpp submissions.

    参考讨论区,使用队列存储节点指针。每次while循环开始时,队列中存储了一层的所有节点指针。这时候 Your runtime beats 12.71 % of cpp 

    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> level;
                int size = q.size();
                for (int i = 0; i < size; ++i) {
                    Node *top = q.front();
                    q.pop();
                    level.push_back(top->val);
                    for (int j = 0; j < top->children.size(); ++j) {
                        q.push(top->children[j]);
                    }
                }
    //            for (int i = 0; i < level.size(); ++i)
    //                cout << level[i] << " ";
    //            cout << endl;
                res.push_back(level);
            }
            return res;
        }
    };

     

  • 相关阅读:
    【洛谷P1005】矩阵取数游戏
    【洛谷P1966】火柴排队
    【题解】洛谷P1731 [NOI1999] 生日蛋糕(搜索+剪枝)
    【题解】洛谷P3627 [APIO2009]抢掠计划(缩点+SPFA)
    【题解】洛谷P1262 间谍网络 (强连通分量缩点)
    【题解】洛谷P3200 [HNOI2009] 有趣的数列(卡特兰数+质因数分解)
    点双连通分量 [HNOI2012]矿场搭建
    分块 公主的朋友
    [置顶] 数学的坑,一点点来填
    大暴搜 [NOIP2009]靶形数独
  • 原文地址:https://www.cnblogs.com/learning-c/p/9787893.html
Copyright © 2020-2023  润新知