/**
* 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> > zigzagLevelOrder(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > vv;
if(!root)
return vv;
vector<int> v;
stack<TreeNode*> current,next;
current.push(root);
bool ltr=true;
while(!current.empty()){
TreeNode* tmp=current.top();
current.pop();
v.push_back(tmp->val);
if(ltr){
if(tmp->left)
next.push(tmp->left);
if(tmp->right)
next.push(tmp->right);
} else{
if(tmp->right)
next.push(tmp->right);
if(tmp->left)
next.push(tmp->left);
}
if(current.empty()){
vv.push_back(v);
v.clear();
swap(current,next);
ltr=!ltr;
}
}
return vv;
}
};
Leetcode: Binary Tree Zigzag Level Order Traversal
最新推荐文章于 2019-04-11 10:03:05 发布
