leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历
// 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字形层次遍历的更多相关文章
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- [leetcode] 103 Binary Tree Zigzag Level Order Traversal (Medium)
原题链接 题目要求以"Z"字型遍历二叉树,并存储在二维数组里. 利用BFS,对每一层进行遍历.对于每一层是从左还是从右,用一个整数型判断当前是偶数行还是奇数行就可以了. class ...
- Leetcode#103 Binary Tree Zigzag Level Order Traversal
原题地址 基本数据结构操作,二叉树的层次遍历. 代码: vector<vector<int> > zigzagLevelOrder(TreeNode *root) { vect ...
随机推荐
- ElasticSearch生产模式开发模式的区分
ElasticSearch生产模式开发模式的区分 network.host: 0.0.0.0 如果network.host不是localhost就是生产模式, 开发模式中的warning就是生产模式中 ...
- lua 异常 错误处理 pcall
lua 错误处理 匿名函数 if pcall(function () local s=object.data[1]['type'] end) then return object.data[1]['t ...
- 【转载】Socket通讯原理以及TCP、IP三次握手机制分析
要写网络程序就必须用Socket,这是程序员都知道的.而且,面试的时候,我们也会问对方会不会Socket编程?一般来说,很多人都会说,Socket编程基本就是listen,accept以及send,w ...
- ORA-07445 第一參数为:kkqljpmpr
在版本号11.2.0.1.0上,在pl/sql developer中运行一条SQL会导致连接中断,这样的错误要到trace文件夹下找到错误日志文件,再定位.查了一下资料,是这个版本号的bug. D ...
- Mysql整数运算NULL值处理注意点
CleverCode近期在导出报表的时候,在整数做减法的时候,发现整数减去null得到是null.这是一个细节问题,希望大家以后注意. 1 表中的数据 total,used都是整形,同意为空. 2 有 ...
- appium第一个安卓自动化工程
转自:https://university.utest.com/how-to-set-up-your-first-android-automation-project-with-appium/ App ...
- SenTestingKit.framework的报错!
本文转载至http://www.cocoachina.com/ask/questions/show/106912 ld: building for iOS Simulator, but linking ...
- Bootstrap progress-bar
1.进度条 在网页中,进度条的效果并不少见,比如一个评分系统,比如加载状态等.就如下图所示的一个评分系统,他就是一个简单的进度条效果: 进度条和其他独立组件一样,开发者可以根据自己的需要,选择对应的版 ...
- 通俗易懂EJB
摘自:http://blog.csdn.net/jojo52013145/article/details/5783677 1. 我们不禁要问,什么是"服务集群"?什么是" ...
- NSString类的方法实现
创建一个新字符串并将其设置为 path 指定的文件的内容,使用字符编码enc,在error上返回错误 + (id)stringWithContentsOfURL:(NSURL *)url encodi ...