【leetcode】429. N-ary Tree Level Order Traversal
problem
429. N-ary Tree Level Order Traversal
solution1:Iteration
/* // Definition for a Node. class Node { public: int val; vector<Node*> children; Node() {} Node(int _val, vector<Node*> _children) { val = _val; children = _children; } }; */ class Solution { public: vector<vector<int>> levelOrder(Node* root) { if(root==NULL) return vector<vector<int>>();// vector<vector<int>> res; queue<Node*> q; q.push(root); while(!q.empty()) { int size = q.size(); vector<int> curLevel; for(int i=0; i<size; ++i) { Node* tmp = q.front(); q.pop(); curLevel.push_back(tmp->val); for(auto a:tmp->children) { q.push(a); } } res.push_back(curLevel); } return res; } };
参考
1. Leetcode_429. N-ary Tree Level Order Traversal;
完
各美其美,美美与共,不和他人作比较,不对他人有期待,不批判他人,不钻牛角尖。
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/
心正意诚,做自己该做的事情,做自己喜欢做的事情,安静做一枚有思想的技术媛。
版权声明,转载请注明出处:https://www.cnblogs.com/happyamyhope/