Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
/**
* 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) {
queue <TreeNode *> m_t;
vector <int> lresult;
vector < vector <int> > result;
TreeNode *temp;
int lnum = 0;
if (root == NULL)
{
return result;
}
m_t.push(root);
while(!m_t.empty())
{
lnum = m_t.size();
for (unsigned int i = 0; i < lnum; i++)
{
temp = m_t.front();
lresult.push_back(temp->val);
if (temp->left != NULL)
m_t.push(temp->left);
if (temp->right != NULL)
m_t.push(temp->right);
m_t.pop();
}
result.push_back(lresult);
lresult.clear();
}//此时,输出结果为从根开始一直到叶子节点
vector < vector <int> > result1;//
for(int i = result.size() - 1; i >= 0; i--)
{
result1.push_back(result[i]);
}
result.clear();
return result1;//此时输出的结果为从叶子节点开始,一直到根
}
};