107. Binary Tree Level Order Traversal II

xiaoxiao2021-02-28  62

class Solution { public: queue<TreeNode*> q; stack<vector<int>*> s; vector<vector<int>> ans; vector<vector<int>> levelOrderBottom(TreeNode* root) { if(!root) return ans; q.push(root); while(!q.empty()){ int sz = q.size(); vector<int> *temp = new vector<int>(); while(sz--){ TreeNode *p = q.front(); q.pop(); temp->push_back(p->val); if(p->left) q.push(p->left); if(p->right) q.push(p->right); } s.push(temp); } while(!s.empty()){ ans.push_back(*s.top()); s.pop(); } return ans; } };
转载请注明原文地址: https://www.6miu.com/read-70908.html

最新回复(0)