Binary Tree Zigzag Level Order Traversal

Description

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).

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]
]

Lintcode_ladder

Method

  1. x
  2. x

Example

  1. 1
/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: A list of lists of integer include 
     *          the zigzag level order traversal of its nodes' values 
     */
public:
    // // Version 1, normal BFS then reverse target level seq
    vector<vector<int>> zigzagLevelOrder(TreeNode *root) {
        // write your code here
        vector<vector<int>> result;
        if (!root) {
            return result;
        }
        queue<TreeNode*> q;
        q.push(root);
        int count = 1;
        int depth = 0;
        while (!q.empty()) {
            TreeNode* cur = q.front();
            q.pop();
            --count;
            if (result.size() < depth + 1) {
                result.push_back(vector<int>(1, cur->val));
            } else {
                result[depth].push_back(cur->val);
            }
            // push right then left
            // make the adjaent level has reversed sequerence 
            if (cur->right) {
                q.push(cur->right);
            }
            if (cur->left) {
                q.push(cur->left);
            }
            // update the level and seq of zigzag
            if (count == 0) {
                if (depth % 2 == 0) {
                    reverse(result[depth].begin(), result[depth].end());
                }
                ++depth;
                count = q.size();
            }
        }
        return result;
    }
    // ---------------------------------------------------------------------
    // Version 2, processing the whole current level
    // For example : 3 9 20 # # 15 7
    // depth = 0 : 3 , push(left,right) 9 20 
    // depth = 1 : 20 9, push(right, left) 7 15 # #
    // depth = 2 : 15 7 
    // In here, we use the stack's reversed seq property
    vector<vector<int>> zigzagLevelOrder(TreeNode* head) {
        vector<vector<int>> result;
        if (!head) {
            return result;
        }
        // init
        int depth = 0;
        stack<TreeNode*> curLevel;
        stack<TreeNode*> nxtLevel;
        stack<TreeNode*> tmp;
        // begin
        curLevel.push(head);
        while (!curLevel.empty()) {
            // we can create tmep container row
            // vector<int> row;
            // Also we can create vector<int> by demand
            if (result.size() < depth + 1) {
                result.push_back(vector<int>());
            }
            while (!curLevel.empty()) {
                TreeNode* cur = curLevel.top();
                curLevel.pop();
                // row.push_back(cur->val);
                result[depth].push_back(cur->val);
                if (depth % 2 != 0) {
                    if (cur->right) {
                        nxtLevel.push(cur->right);
                    }
                    if (cur->left) {
                        nxtLevel.push(cur->left);
                    }
                } else {
                    if (cur->left) {
                        nxtLevel.push(cur->left);
                    }
                    if (cur->right) {
                        nxtLevel.push(cur->right);
                    }
                }
            }
            // result.push_back(row);
            depth++;
            curLevel = nxtLevel;
            nxtLevel = tmp;
        }
        return result;
    }
};

Similar problems

x

Tags

x

results matching ""

    No results matching ""