leetcode
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
std::vector<std::vector<int>> results;
if ( root ) {
std::vector<TreeNode *> nodes = { root };
while ( !nodes.empty() ) {
std::vector<int> arrNumbers;
for ( int index = 0; index < nodes.size(); ++index ) {
TreeNode *node = nodes[index];
if ( node -> left ) {
nodes.insert( nodes.begin() + index, node -> left);
++index;
}
if ( node -> right ) {
nodes.insert( nodes.begin() + index, node -> right);
++index;
}
if ( node ) {
arrNumbers.push_back( node -> val );
nodes.erase( nodes.begin() + index );
--index;
}
}
if ( arrNumbers.size() > 0 ) {
results.push_back( arrNumbers );
}
}
}
return results;
}
};