[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 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]
]
这个题目的思路也是典型的BFS, 只是每一层分别正序和倒序, 我还是利用102. Binary Tree Level Order Traversal_Medium tag: BFS的思路,只是多加了一个len(ans)%2== 0 去判断是应该正序还是倒序, 如果是倒序, 就把temp reverse然后append进入ans里面即可.
1. Constraints
1) edge case, root is None
2. Ideas
BFS: T: O(n) S: O(n)
3.code:
class Solution:
def zigzagLevel(self, root):
ans = []
if not root: return ans # edge case
queue = collections.deque([root])
while queue:
size, level = len(queue), []
for _ in range(size):
node = queue.popleft()
level.append(node.val)
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
if len(ans)%2 ==0:
ans.append(level)
else:
ans.append(level[::-1])
return ans
[LeetCode] 103. Binary Tree Zigzag Level Order Traversal _ Medium tag: BFS的更多相关文章
- [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 ...
- 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 (二叉树Z字形水平序) 解题思路和方法
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 ...
- [leetcode]103. Binary Tree Zigzag Level Order Traversal二叉树Z字形层序遍历
相对于102题,稍微改变下方法就行 迭代方法: 在102题的基础上,加上一个变量来判断是不是需要反转 反转的话,当前list在for循环结束后用collection的反转方法就可以实现反转 递归方法: ...
- leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历
// 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...
随机推荐
- 支持AIRPLAY ,DLNA,MIRACAST的HDMI DONGLE
好吧,今天没节操,帮老婆推广一下淘宝的店. 联我影棒 http://item.taobao.com/item.htm?spm=a230r.1.14.132.jqGLCa&id=36476326 ...
- 例说hg(五)————创建repository
本文由博主原创,转载请注明出处(保留此处和链接): IT人生(http://blog.csdn.net/robinblog/article/details/17933747) 有很多网站提供了免费的M ...
- 【大数据系列】Hadoop DataNode读写流程
DataNode的写操作流程 DataNode的写操作流程可以分为两部分,第一部分是写操作之前的准备工作,包括与NameNode的通信等:第二部分是真正的写操作. 一.准备工作 1.首先,HDFS c ...
- GitHub 在使用命令行 git push 时报错:The requested URL returned error: 403
使用 git 的命令行向 GitHub 提交的时候,报错: [Young@localhost OtherLang]$ git push origin master error: The request ...
- LeetCode 11 Container With Most Water(分支判断问题)
题目链接 https://leetcode.com/problems/container-with-most-water/?tab=Description Problem: 已知n条垂直于x轴的线 ...
- css零零散散的笔记
1.div根据内容自适应大小 效果图: html: <body> <div class="parent"> <div class="chil ...
- 新浪的动态策略灰度发布系统:ABTestingGateway
原文链接:http://www.open-open.com/lib/view/open1439889185239.html ABTesingGateway 是一个可以动态设置分流策略的灰度发布系统,工 ...
- 【BZOJ1814】Ural 1519 Formula 1 插头DP
[BZOJ1814]Ural 1519 Formula 1 题意:一个 m * n 的棋盘,有的格子存在障碍,求经过所有非障碍格子的哈密顿回路个数.(n,m<=12) 题解:插头DP板子题,刷板 ...
- ThinkPHP widge使用示例
1.widge一般用于公用模块的设计与使用,以便加强软件模块的复用性与重用性 一般使用include方法设计公共模块,比如<include file="home:header" ...
- 1.3tf的varible\labelencoder
1.tf的varible变量 import tensorflow as tf #定义变量--这里是计数的变量 state=tf.Variable(0,name='counter') print (st ...