LeetCode: 103. Binary Tree Zigzag Level Order Traversal

LeetCode: 103. 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,null,null,15,7],

    3
   / \   9  20
    /  \    15   7

return its zigzag level order traversal as:

[
  [3],
  [20,9],
  [15,7]
]

题目大意: 按照 “之字形” 层次遍历二叉树。

解题思路

  • 方法一: 使用 LeetCode: 102. Binary Tree Level Order Traversal 的方法。在偶数次写入时,翻转写入的序列。
  • 方法二: 使用两个栈存每行的数据,奇数行先存左孩子,再存右孩子,偶数行先存右孩子,再存左孩子。

AC 代码

  • 方法一
/** * Definition for a binary tree node. * 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>> zigzagLevelOrderSequence;         // 保存层次遍历序列
        vector<int>         curLevel;                         // 保存当前层的序列

        queue<TreeNode*> que;   // BFS 队列
        que.push(root);         // 将根节点加入 BFS 对列
        que.push(nullptr);      // 用 nullptr 来隔开每一层的元素

        bool flag = false;      // 标记是否反转

        while(!que.empty())
        {
            TreeNode* curNode = que.front();
            que.pop();

            if(curNode == nullptr && curLevel.empty())
            {
                continue;
            }
            else if(curNode == nullptr && !curLevel.empty())
            {
                que.push(nullptr);
                if(flag == true) reverse(curLevel.begin(), curLevel.end());
                zigzagLevelOrderSequence.push_back(curLevel);
                curLevel.clear();

                flag = !flag;
            }
            else
            {
                curLevel.push_back(curNode->val);
                if(curNode->left != nullptr) que.push(curNode->left);
                if(curNode->right != nullptr) que.push(curNode->right);
            }
        }

        return zigzagLevelOrderSequence;
    }
};
  • 方法二:
/** * Definition for a binary tree node. * 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) {
        if(root == nullptr) return {};
        vector<int> curLevelOrderSeq;                   // 当前层的 zigzag 序列
        vector<vector<int>> zigzagLevelOrderSeq;        // 整棵树的 zigzag 序列

        stack<TreeNode*> curLevel;      // 当前处理层的节点
        stack<TreeNode*> nextLevel;     // 下一次处理层的节点

        // 根节点加入第一层
        nextLevel.push(root);
        bool flag = true;

        while(!nextLevel.empty())
        {
            curLevel = nextLevel;
            nextLevel = stack<TreeNode*>();

            while(!curLevel.empty())
            {
                TreeNode* curNode = curLevel.top();
                curLevel.pop();

                curLevelOrderSeq.push_back(curNode->val);
                if(curNode->left && flag) nextLevel.push(curNode->left);
                if(curNode->right) nextLevel.push(curNode->right);
                if(curNode->left && !flag) nextLevel.push(curNode->left);
            }

            zigzagLevelOrderSeq.push_back(curLevelOrderSeq);
            curLevelOrderSeq.clear();
            flag = !flag;
        } 

        return zigzagLevelOrderSeq;
    }
};
全部评论

相关推荐

像好涩一样好学:这公司我也拿过 基本明确周六加班 工资还凑活 另外下次镜头往上点儿
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务