這個題目貌似大家都會啊,哈哈,的確把它歸納爲中等是不合格的
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 * @return int整型vector<vector<>> */ vector<vector<int> > levelOrder(TreeNode* root) { // write code here vector<vector<int> > res; orderTreeByLevel(0, root,res); return res; } void orderTreeByLevel(int level,TreeNode *node,vector<vector<int> > &res){ if(node == NULL){ return; }else{ if(res.size() <= level){ res.push_back({}); } res[level].push_back(node->val); } orderTreeByLevel(level+1, node->left, res); orderTreeByLevel(level+1, node->right, res); } };