标签:遍历 info 题目 span tor div return cto inf
题目:
解法:
广度优先遍历就好。
1 /* 2 // Definition for a Node. 3 class Node { 4 public: 5 int val; 6 vector<Node*> children; 7 8 Node() {} 9 10 Node(int _val) { 11 val = _val; 12 } 13 14 Node(int _val, vector<Node*> _children) { 15 val = _val; 16 children = _children; 17 } 18 }; 19 */ 20 21 class Solution { 22 public: 23 vector<vector<int>> levelOrder(Node* root) 24 { 25 if(NULL == root) 26 { 27 return {}; 28 } 29 30 vector<vector<int>> ans; 31 32 queue<Node*> que; 33 que.push(root); 34 35 while(!que.empty()) 36 { 37 vector<int> level; 38 for(int i=que.size(); i ; i--) 39 { 40 //压入当前层 41 Node* curr=que.front(); 42 que.pop(); 43 level.push_back(curr->val); 44 45 for(Node* it:curr->children) 46 { 47 que.push(it); 48 } 49 } 50 ans.push_back(level); 51 } 52 return ans; 53 } 54 };
标签:遍历 info 题目 span tor div return cto inf
原文地址:https://www.cnblogs.com/ocpc/p/12821362.html