/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > ans;
if (root == NULL)
{
return ans;
}
TreeNode *ptr = NULL;
TreeNode *pEnd = root;
queue<TreeNode *> q;
q.push(root);
vector<int> vec;
while (!q.empty())
{
ptr = q.front();
q.pop();
vec.push_back(ptr->val);
if (ptr->left != NULL)
{
q.push(ptr->left);
}
if (ptr->right != NULL)
{
q.push(ptr->right);
}
if (ptr == pEnd)
{
ans.push_back(vec);
vec.clear();
pEnd = q.back();
}
}
reverse(ans.begin(), ans.end());
return ans;
}
};
LeetCode-Binary Tree Level Order Traversal II
最新推荐文章于 2024-11-14 20:04:48 发布