【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

来源: https://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).

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]

]

(二)解题

题目大意:给定一个二叉树,按层序遍历输出,层数从1开始,奇数层从左往右输出,偶数层从右往左输出。

解题思路:上一题【一天一道LeetCode】#102. Binary Tree Level Order Traversal采用queue的数据结构来层序输出,每层都是按从左往右的顺序输出,所以,这一题可以采用deque的数据结构,根据奇数和偶数层来判断输出顺序。

详细解释见代码:

/**
 * 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>> ret;
        if(root==NULL) return ret;
        deque<TreeNode*> deq;//用来存放每一层的节点
        deq.push_back(root);//将根节点放入queue等待处理
        int n = 1;//记录层数
        while(!deq.empty())
        {
            vector<int> tempnode;
            deque<TreeNode*> temp;//存放下一层的节点
            while(!deq.empty()){
                if(n%2==1)//奇数层
                {
                    TreeNode* tn = deq.front();//从头开始取节点
                    tempnode.push_back(tn->val);
                    deq.pop_front();
                    if(tn->left!=NULL) temp.push_back(tn->left);//从左往右放入节点
                    if(tn->right!=NULL) temp.push_back(tn->right);
                }
                else//偶数层
                {
                    TreeNode* tn = deq.back();//从尾部开始取节点
                    tempnode.push_back(tn->val);
                    deq.pop_back();
                    if(tn->right!=NULL) temp.push_front(tn->right);//从右往左放入节点
                    if(tn->left!=NULL) temp.push_front(tn->left);
                }

            }
            deq = temp;
            ret.push_back(tempnode);
            n++;//处理下一层
        }
        return ret;
    }
};
时间: 2024-10-29 19:06:36

【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal的相关文章

Leetcode 103. Binary Tree Zigzag Level Order Traversal

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<vector<int>>

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

Java for 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,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

[LeetCode] 103. Binary Tree Zigzag Level Order Traversal Java

题目: 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 retur

19.3.5 [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 it

Leetcode 树 Binary Tree Zigzag Level Order Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Zigzag Level Order Traversal Total Accepted: 8678 Total Submissions: 33047My Submissions Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from le

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51524241 Subject 出处:https://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 ri

[leetcode tree]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 it

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 it