// 103. Binary Tree Zigzag Level Order Traversal

// https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/

// swift

// Definition for a binary tree node.

public class TreeNode {

    public var val: Int

    public var left: TreeNode?

    public var right: TreeNode?

    public init(_ val: Int) {

        self.val = val

        self.left = nil

        self.right = nil

    }

}

class Solution {

    func zigzagLevelOrder(_ root: TreeNode?) -> [[Int]] {

        guard root != nil else {

            return [[Int]]()

        }

        func nextLevelResult(_ nodes: [TreeNode], flag: Bool) -> [TreeNode] {

            var result = [TreeNode]()

            for node in nodes.reversed() {

                if flag {

                    if (node.left != nil) {

                        result.append(node.left!)

                    }

                    if (node.right != nil) {

                        result.append(node.right!)

                    } 

                } else {

                    if (node.right != nil) {

                        result.append(node.right!)

                    }

                    if (node.left != nil) {

                        result.append(node.left!)

                    }

                }

            }

            return result

        }

        var result = [[TreeNode]]()

        var temp = [root!]

        var flag = false

        while temp.count > 0 {

            result .append(temp)

            temp = nextLevelResult(temp, flag: flag)

            flag = !flag

        }

        return result.map { nodes in

            nodes.map { node in

                node.val

            }

        }

    }

}

leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历的更多相关文章

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

  2. [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现

    Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes ...

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

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

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

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

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

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

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

  9. Leetcode#103 Binary Tree Zigzag Level Order Traversal

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

随机推荐

  1. [转] twemproxy ketama一致性hash分析

    评注:提到HAProxy业务层proxy, twemproxy存储的proxy. 其中还提到了ketama算法的实现源码 转自:http://www.cnblogs.com/basecn/p/4288 ...

  2. centos 7 -- Disk Requirements: At least 134MB more space needed on the / filesystem.

    用了幾年的centos7,今天執行yum update時,彈出一行有錯誤的提示:Disk Requirements:   At least 134MB more space needed on the ...

  3. 走入asp.net mvc不归路:[1]项目文件结构

    先来了解一下一个asp.net mvc项目的文件结构. 1 项目文件结构一览 2 mvc,顾名思义,一个项目中最重要的就是这三个东西:M(Model,模型),V(View,视图),C(Controll ...

  4. HDU 1085 Holding Bin-Laden Captive!(母函数,或者找规律)

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  5. HDU 5366:The mook jong 递推

    The mook jong  Accepts: 506  Submissions: 1281  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit ...

  6. MeiTuanLocateCity

    https://github.com/eltld/MeiTuanLocateCity

  7. c#生成PDF准考证

    项目中需要做一个生成PDF准考证的功能,在这里跟大家分享一下思路.. 1.首先是下载Adobe Acrobat 9 Pro,安装破解(高版本的貌似破解,不了,自带正版意识的略过..随意下载) 2.新建 ...

  8. 基于第三方微信授权登录的iOS代码分析

    本文转载至 http://www.cocoachina.com/ios/20140922/9715.html 微信已经深入到每一个APP的缝隙,最常用的莫过分享和登录了,接下来就以代码的形式来展开微信 ...

  9. socketIO原理图

  10. react源码分析

    ReactMount.render -> ReactMount._renderSubtreeIntoContainer -> ReactMount._renderNewRootCompon ...