class Solution {
public:
vector<vector<int> > levelOrderBottom(TreeNode *root) {
vector<TreeNode> path;
vector<int>path_val;
vector<vector<int>> res;
if(root==NULL)
return res;
path.push_back(*root);
path_val.push_back(root->val);
res.push_back(path_val);
find(path,res);
reverse(res.begin(),res.end());
return res;
}
void find(vector<TreeNode>&path,vector<vector<int>>&res)
{
vector<int>path_val;
vector<TreeNode>temppath;
for(int i=0;i<path.size();i++)
{
if(path[i].left!=NULL)
temppath.push_back(*path[i].left);
if(path[i].right!=NULL)
temppath.push_back(*path[i].right);
}
if(temppath.size()==0)
return ;
path.clear();
for(int i=0;i<temppath.size();i++)
{
path_val.push_back(temppath[i].val);
path.push_back(temppath[i]);
}
res.push_back(path_val);
find(path,res);
return ;
}
};
Binary Tree Level Order Traversal II
最新推荐文章于 2019-06-27 15:08:13 发布