Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<vector<int>> levelOrder(TreeNode* root) {
- if(root==NULL){
- return {};
- }
- queue<TreeNode*> q;
- q.push(root);
- vector<vector<int>> v;
- v.push_back({root->val});
- while(q.size()!=0){
- int n = q.size();
- vector<int> s;
- for(int i=0; i<n; i++){
- if(q.front()->left!=NULL){
- q.push(q.front()->left);
- s.push_back(q.front()->left->val);
- }
- if(q.front()->right!=NULL){
- q.push(q.front()->right);
- s.push_back(q.front()->right->val);
- }
- q.pop();
- }
- v.push_back(s);
- }
- v.erase(v.end()-1);
- return v;
- }
- };
Add Comment
Please, Sign In to add comment