BFS,没啥好说的。
class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> ret; queue<pair<TreeNode*, int>> que; // TreeNode*, depth if(root) que.push({root, 0}); while(!que.empty()) { auto top = que.front(); que.pop(); if((int)ret.size() - 1 < top.second) ret.push_back({top.first -> val}); else { ret.back().emplace_back(top.first -> val); } if(top.first -> left) que.push({top.first -> left, top.second + 1}); if(top.first -> right) que.push({top.first -> right, top.second + 1}); } return ret; } };