一天一道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;
    }
};

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

  1. [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 ...

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

  3. [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 ...

  4. [LeetCode] 103. Binary Tree Zigzag Level Order Traversal _ Medium tag: BFS

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

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

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

  7. Leetcode#103 Binary Tree Zigzag Level Order Traversal

    原题地址 基本数据结构操作,二叉树的层次遍历. 代码: vector<vector<int> > zigzagLevelOrder(TreeNode *root) { vect ...

  8. [leetcode] 103 Binary Tree Zigzag Level Order Traversal (Medium)

    原题链接 题目要求以"Z"字型遍历二叉树,并存储在二维数组里. 利用BFS,对每一层进行遍历.对于每一层是从左还是从右,用一个整数型判断当前是偶数行还是奇数行就可以了. class ...

  9. [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树Z字形层序遍历

    相对于102题,稍微改变下方法就行 迭代方法: 在102题的基础上,加上一个变量来判断是不是需要反转 反转的话,当前list在for循环结束后用collection的反转方法就可以实现反转 递归方法: ...

  10. leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历

    // 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...

随机推荐

  1. Tensorflow 基于分层注意网络的文件分类器

    After the exercise of building convolutional, RNN, sentence level attention RNN, finally I have come ...

  2. 一个使用 Python 的人工智能聊天机器人框架

    一个Python 的 AI Chatbot框架 建立一个聊天室可以听起来很棒,但它是完全可行的. IKY是一个内置于Python中的AI动力对话对话界面. 使用IKY,很容易创建自然语言会话场景,无需 ...

  3. Debugging TensorFlow models 调试 TensorFlow 模型

    Debugging TensorFlow models Symbolic nature of TensorFlow makes it relatively more difficult to debu ...

  4. Restful中 @RequestParam,@PathParam,@PathVariable等注解区别

    @RequestParam 和 @PathVariable 注解是用于从request中接收请求的,两个都可以接收参数,关键点不同的是@RequestParam 是从request里面拿取值,而 @P ...

  5. tree的遍历--广度优先遍历

    一.二叉树demo var tree = { value: '一', left: { value: '二', left: { value: '四', right: { value: '六' } } } ...

  6. JavaScript 函数定义

    JavaScript 使用关键字 function 定义函数. 函数可以通过声明定义,也可以是一个表达式. 函数声明 在之前的教程中,你已经了解了函数声明的语法 : function function ...

  7. 用豆瓣镜像解决pip安装慢的问题

    pip3 install django==1.9 -i http://pypi.douban.com/simple/

  8. 20160211.CCPP体系详解(0021天)

    程序片段(01):01.指针数组.c+02.动态数组.c 内容概要:指针数组 ///01.指针数组.c #include <stdio.h> #include <stdlib.h&g ...

  9. Programming In Scala笔记-第七章、Scala中的控制结构

    所谓的内建控制结构是指编程语言中可以使用的一些代码控制语法,如Scala中的if, while, for, try, match, 以及函数调用等.需要注意的是,Scala几乎所有的内建控制结构都会返 ...

  10. Android简易实战教程--第四十六话《RecyclerView竖向和横向滚动》

    Android5.X后,引入了RecyclerView,这个控件使用起来非常的方便,不但可以完成listView的效果,而且还可以实现ListView无法实现的效果.当然,在新能方便也做了大大的提高. ...