class Solution {
public:
vector<vector<int> >result;
vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
vector<int> v;
vector<TreeNode*> node;
if(root == NULL)
return result;
node.push_back(root);
int begin = 0,end;
int cnt = 0;
while(begin < node.size()){
end = node.size();
v.clear();
while(begin < end){
TreeNode * root = node[begin];
v.push_back(root -> val);
if(root -> left != NULL)
node.push_back(root -> left);
if(root -> right != NULL)
node.push_back(root -> right);
begin ++;
}
if(cnt%2 == 1){
reverse(v.begin(),v.end());
}
result.push_back(v);
cnt ++;
}
return result;
}
};
[LeetCode] Binary Tree Zigzag Level Order Traversal
最新推荐文章于 2020-06-07 02:08:27 发布