N-ary Tree Level Order Traversal
Posted liuqiujie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了N-ary Tree Level Order Traversal相关的知识,希望对你有一定的参考价值。
/* // 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) { queue<Node*> q; vector<vector<int>> vec; if(!root) return vec; q.push(root); while(!q.empty()) { vector<int> tmp; for(int size=q.size();size;--size) { Node* node=q.front();q.pop(); tmp.push_back(node->val); for(auto& child:node->children) q.push(child); } vec.push_back(tmp); } return vec; } };
以上是关于N-ary Tree Level Order Traversal的主要内容,如果未能解决你的问题,请参考以下文章
429. N-ary Tree Level Order Traversal
429. N-ary Tree Level Order Traversal
429. N-ary Tree Level Order Traversal
429. N-ary Tree Level Order Traversal - Easy