leetcode102 Binary Tree Level Order Traversal
"""
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
] """
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution:
def levelOrder(self, root):
if root == None: #树为空,直接返回[]
return []
queue = [root]
cur = []
res = []
while queue:
cur = [x.val if x else None for x in queue] #存当前层的value
newqueue = [] #newqueue用来遍历下一层使用
for x in queue:
if x.left: #bug 如果不判断,结果包含[None]
newqueue.append(x.left)
if x.right:
newqueue.append(x.right)
queue = newqueue
res.append(cur) #将当前[]append到最后结果
return res
"""
本题与leetcode101相似,用BFS可解
"""
leetcode102 Binary Tree Level Order Traversal的更多相关文章
- 剑指offer从上往下打印二叉树 、leetcode102. Binary Tree Level Order Traversal(即剑指把二叉树打印成多行、层序打印)、107. Binary Tree Level Order Traversal II 、103. Binary Tree Zigzag Level Order Traversal(剑指之字型打印)
从上往下打印二叉树这个是不分行的,用一个队列就可以实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode ...
- LeetCode102 Binary Tree Level Order Traversal Java
题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ri ...
- (二叉树 BFS) leetcode102. Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树
题目 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 ...
- Leetcode102. Binary Tree Level Order Traversal二叉树的层次遍历
给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其 ...
- (二叉树 BFS) leetcode 107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【leetcode】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
随机推荐
- nginx 网络层的优化
TCP三次握手四次挥手 系统层的优化,主动建立连接时的重试次数 net.ipv4.tcp_syn_retries = 6 建立连接时本地端口可用范围:手动可以tiaoz net.ipv4.ip_loc ...
- 【转载】 NVIDIA Tesla/Quadro和GeForce GPU比较
原文地址: https://blog.csdn.net/m0_37462765/article/details/74394932 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议 ...
- 5-3 使用antDesign的form组件
import { Form, Icon, Input, Button, Checkbox } from 'antd'; class NormalLoginForm extends React.Comp ...
- Go Start
一.安装 下载解压后,配置PATH tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz export PATH=$PATH:/usr/local/go ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:缩写
<!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 缩写</title> <lin ...
- oracle SQL 练习
COURSE 表 DROP TABLE "SCOTT"."course"; CREATE TABLE "SCOTT"."cours ...
- 本机配置集群主机名访问(Windows配置hosts)
Windows配置hosts C:\Windows\System32\drivers\etc\hosts 主机IP 主机名 示例: 192.168.1.1 master 192.168.1.2 sla ...
- Java中四种遍历Map对象的方法
方法一:在for-each循环中使用entry来遍历,通过Map.entrySet遍历key和value,这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Int ...
- NLP的比赛和数据集
整理了NLP领域的比赛.数据集.模型 比赛 网站 主办方(作者) decaNLP http://decanlp.com/ Salesforce CLUE https://github.com/CLUE ...
- 帆软FineReport报表使用小技巧
1.IF函数写法: =IF(E3=0 && F3=0 && G3=0,1,0)