zoukankan      html  css  js  c++  java
  • 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;
        }
    };

     

  • 相关阅读:
    正则表达式详解<一>
    multimap详讲
    map详讲<二>
    map详解<一>
    priority_queue详解
    容器适配器(一):queue
    用 input() 函数返回的数据是字符串类型
    学习python的基本了解
    学习oracle的SQL语句 练习
    oracle 练习题
  • 原文地址:https://www.cnblogs.com/learning-c/p/9787893.html
Copyright © 2011-2022 走看看