103. 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).
For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its zigzag level order traversal as:[[3], [20,9], [15,7]]
分析:这道题是Level Order的变形,Zigzag的遍历。
解法一:最简单的思路,上一题是lever order traversal我们可以采取相同的方法将结果稍加处理即可。对所有奇数的数组元素进行倒序。

class Solution {
private:
    vector<vector<int>> ret;

    void reverse_array(vector<int>& arr)
    {
        for(int i = 0 , j = arr.size() - 1; i < j; i++, j--)
            swap(arr[i], arr[j]);
    }

    void dfs_traversal(TreeNode* root, int depth)
    {
        if(root == nullptr)
            return;

        if(ret.size() < depth)
            ret.push_back(vector<int>());

        ret[depth - 1].push_back(root->val);

        dfs_traversal(root->left, depth + 1);
        dfs_traversal(root->right, depth +1);
    }

public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        dfs_traversal(root, 1);

        for(int i = 1; i < ret.size(); i += 2)
            reverse_array(ret[i]);

        return ret;
    }
};

解法二:利用栈先进后出的特点,设置leftStack和rightStack,leftStack从左到右输出val,同时将其子树传到rightStack,我们遍历rightStack时候恰好是从右向左。
代码:

class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        if(root == nullptr)
            return ret;
        stack leftStack;
        stack rightStack;
        leftStack.push(root);        

        while(!leftStack.empty() || !rightStack.empty())
        {
            TreeNode* t = nullptr;
            vector<int> tmp;

            while(!leftStack.empty())
            {
                t = leftStack.top();
                leftStack.pop();
                tmp.push_back(t->val);
                if(t->left)
                    rightStack.push(t->left);
                if(t->right)
                    rightStack.push(t->right);
            }

            if(!tmp.empty())
                ret.push_back(tmp);
            tmp.clear();

            while(!rightStack.empty())
            {
                t = rightStack.top();
                rightStack.pop();
                tmp.push_back(t->val);
                if(t->right)
                    leftStack.push(t->right);
                if(t->left)
                    leftStack.push(t->left);
            }

            if(!tmp.empty())
                ret.push_back(tmp);
            tmp.clear();
        }

        return ret;
    }
};

解法三:解法一的优化,我们出入数组的时候就进行处理。当depth是奇数的时候将val直接push_back,是偶数的时候将val用insert到前面。
代码如下:

class Solution {
private:
    vector<vector<int>> ret;
    void dfs_traversal(TreeNode* root, int depth)
    {
        if(root == nullptr)
            return;

        if(ret.size() < depth)
            ret.push_back(vector<int>());

        if(depth % 2 == 1)
            ret[depth - 1].push_back(root->val);
        else
            ret[depth - 1].insert(ret[depth - 1].begin(), root->val);

        dfs_traversal(root->left, depth + 1);
        dfs_traversal(root->right, depth +1);
    }

public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        dfs_traversal(root, 1);

        return ret;
    }
};

你必须非常努力,才能显得毫不费力!!!

你可能感兴趣的:(LeetCodec++刷题札记)