C++ solution (Using level order traversal + reverse)


#1

void levelorder(TreeNode *root,vector &ans){
if(root==NULL)
return;
queue<TreeNode *> q;
q.push(root);
while(!q.empty()){
int count=q.size();
for(int i=0;i<count;i++){
TreeNode curr=q.front();
q.pop();
ans.push_back(curr->val);
if(curr->right!=NULL)
q.push(curr->right);
if(curr->left!=NULL)
q.push(curr->left);
}
}
}
vector Solution::solve(TreeNode
A) {
vector ans;
TreeNode *root=A;
levelorder(root,ans);
reverse(ans.begin(),ans.end());
return ans;
}