Skip to content

Instantly share code, notes, and snippets.

@walkingtospace
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkingtospace/63f8c98fb34d6463ec0f to your computer and use it in GitHub Desktop.
Save walkingtospace/63f8c98fb34d6463ec0f to your computer and use it in GitHub Desktop.
binary-tree-level-order-traversal-ii
https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > levelOrderBottom(TreeNode *root) {
vector<vector<int>> res;
if(root == NULL) {
return res;
}
queue<TreeNode*> queue;
queue.push(root);
while(!queue.empty()) {
int width = queue.size();
vector<int> breadth;
for(int i=0; i<width ; ++i) {
TreeNode* temp = queue.front();
queue.pop();
breadth.push_back(temp->val);
if(temp->left) {
queue.push(temp->left);
}
if(temp->right) {
queue.push(temp->right);
}
}
res.push_back(breadth);
}
std::reverse(res.begin(), res.end());
return res;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment