/**
* 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> > levelorder;
if (!root) {
return levelorder;
}
vector<TreeNode *> seq;
TreeNode *cur;
vector<int> level;
seq.push_back(root);
int begin = 0;
int end;
while (begin < seq.size()) {
end = seq.size();
while (begin < end) {
cur = seq[begin];
if (cur->left) {
seq.push_back(cur->left);
}
if (cur->right) {
seq.push_back(cur->right);
}
level.push_back(cur->val);
begin++;
}
levelorder.push_back(level);
level.clear();
}
reverse(levelorder.begin(), levelorder.end());
return levelorder;
}
};
LeetCode Binary Tree Level Order Traversal II
最新推荐文章于 2018-03-24 20:32:25 发布