[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes‘ values. (ie, from left to right, then right to left for the next level and alternate between).

For example: Given binary tree {3,9,20,#,#,15,7},

    3
   /   9  20
    /     15   7

return its zigzag level order traversal as:

[
  [3],
  [20,9],
  [15,7]
]
/**
 * 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) {
        vector<vector<int> > res;
        if(root==NULL)
            return res;
        queue<TreeNode *> q;
        q.push(root);
        q.push(NULL);
        res = bfs(q);
        return res;
    }
private:
    vector<vector<int> > bfs(queue<TreeNode *> q){
        vector<vector<int> > res;
        vector<int> temp;
        bool needReverse = false;

        while(!q.empty()){
            TreeNode *p = q.front();
            q.pop();
            if(p!=NULL){
                temp.push_back(p->val);
                if(p->left!=NULL){
                    q.push(p->left);
                }
                if(p->right!=NULL){
                    q.push(p->right);
                }
            }else if(p==NULL ){

                if(needReverse){
                    int len = temp.size();
                    for(int i=0,j=len-1;i<j;i++,j--)
                        swap(temp[i],temp[j]);
                }
                res.push_back(temp);
                temp.clear();
                needReverse = (!needReverse);
                if(!q.empty())
                   q.push(NULL);
            }

        }//end while
        return res;
    }//end bfs
};

方法:用queue实现bfs,对树按层进行搜索,树的结点按层存入queue中,用NULL值标记本层的结束。用bool值按层取反决定本层的vector是否要逆置。

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs),布布扣,bubuko.com

时间: 2024-12-11 13:44:56

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)的相关文章

【leetcode】Binary Tree Zigzag Level Order Traversal (middle)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zigz

[leetcode]Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#

[leetcode]Binary Tree Zigzag Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between

leetcode_103题——Binary Tree Zigzag Level Order Traversal(广度优先搜索,队列queue,栈stack)

Binary Tree Zigzag Level Order Traversal Total Accepted: 31183 Total Submissions: 117840My Submissions Question Solution Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left fo

leetCode 103.Binary Tree Zigzag Level Order Traversal (二叉树Z字形水平序) 解题思路和方法

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

LeetCode OJ:Binary Tree Zigzag Level Order Traversal(折叠二叉树遍历)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zigz

[LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zigz

LeetCode :: Binary Tree Zigzag Level Order Traversal [tree, BFS]

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

LeetCode—Binary Tree Zigzag Level Order Traversal 二叉树的Z扫描

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig