class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root)
{
vector<vector<int>> result;
recur_levelOrder(result, root, 0);
return result;
}
void recur_levelOrder(vector<vector<int>> &result, TreeNode *root, int level)
{
if (!root)
{
return;
}
if ((level+1) > result.size())
{
result.push_back(vector<int> {});
}
result[level].push_back(root>val);
recur_levelOrder(result, root>left, level+1);
recur_levelOrder(result, root>right, level+1);
}
};
C++ 4ms simple recursive solution


class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int> > result; levelOrder_recur(result, root, 0); return result; } void levelOrder_recur(vector<vector<int> >& result, TreeNode* root, int level) { if (!root) { return; } if (level > (result.size()1)) { result.push_back(vector<int>{}); } result[level].push_back(root > val); levelOrder_recur(result, root > left, level+1); levelOrder_recur(result, root > right, level+1); } };

@scnuysc because result.size() is an unsigned int, and the result of result.size()1 is unsigned too. So when result is empty, result.size() is zero, and result.size()1 is not 1 but a large number. If you use int s = result.size(), when result is empty, the output s  1 is 1, so it will work.