leetcode 103. 二叉树的锯齿形层序遍历(层序遍历)

给定一个二叉树,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。例如:
给定二叉树 [3,9,20,null,null,15,7],3/ \9  20/  \15   7
返回锯齿形层序遍历如下:[[3],[20,9],[15,7]
]

代码

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {public List<List<Integer>> zigzagLevelOrder(TreeNode root) {List<List<Integer>> res=new ArrayList<>();  if(root==null) return res;Queue<TreeNode> queue=new LinkedList<>();//使用队列实现层序遍历queue.add(root);int level=0;while (!queue.isEmpty()){List<Integer> list=new ArrayList<>();int size=queue.size();for(int i=0;i<size;i++)//将同一层节点全部出队,将下一层节点入队{TreeNode treeNode=queue.poll();list.add(treeNode.val);if(treeNode.left!=null) queue.add(treeNode.left);if(treeNode.right!=null) queue.add(treeNode.right);}if((level++)%2==1) Collections.reverse(list);//奇数层的逆转res.add(list);}return res;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392451.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

简单易用的MongoDB

从我第一次听到Nosql这个概念到如今已经走过4个年头了&#xff0c;但仍然没有具体的去做过相应的实践。最近获得一段学习休息时间&#xff0c;购买了Nosql技术实践一书&#xff0c;正在慢慢的学习。在主流观点中&#xff0c;Nosql大体分为4类&#xff0c;键值存储数据库&#x…

html画布图片不显示_如何在HTML5画布上显示图像

html画布图片不显示by Nash Vail由Nash Vail Ok, so here’s a question: “Why do we need an article for this, Nash?”好的&#xff0c;这是一个问题&#xff1a;“为什么我们需要为此写一篇文章&#xff0c;纳什&#xff1f;” Well, grab a seat.好吧&#xff0c;坐下…

java断点续传插件_视频断点续传+java视频

之前仿造uploadify写了一个HTML5版的文件上传插件&#xff0c;没看过的朋友可以点此先看一下~得到了不少朋友的好评&#xff0c;我自己也用在了项目中&#xff0c;不论是用户头像上传&#xff0c;还是各种媒体文件的上传&#xff0c;以及各种个性的业务需求&#xff0c;都能得到…

全栈入门_启动数据栈入门包(2020)

全栈入门I advise a lot of people on how to build out their data stack, from tiny startups to enterprise companies that are moving to the cloud or from legacy solutions. There are many choices out there, and navigating them all can be tricky. Here’s a brea…

Go-json解码到接口及根据键获取值

Go-json解码到接口及根据键获取值 package mainimport ("encoding/json""fmt""github.com/bitly/go-simplejson" )type JsonServer struct {ServerName stringServerIP string }type JsonServers struct {Servers []JsonServer }func main() {…

C#接口的显隐实现

显示接口实现与隐式接口实现 何为显式接口实现、隐式接口实现&#xff1f;简单概括&#xff0c;使用接口名作为方法名的前缀&#xff0c;这称为“显式接口实现”&#xff1b;传统的实现方式&#xff0c;称为“隐式接口实现”。下面给个例子。 IChineseGreeting接口&#xff0c;…

亚马逊 各国站点 链接_使用Amazon S3和HTTPS的简单站点托管

亚马逊 各国站点 链接by Georgia Nola乔治亚诺拉(Georgia Nola) 使用Amazon S3和HTTPS的简单站点托管 (Simple site hosting with Amazon S3 and HTTPS) Hiya folks!大家好&#xff01; In this tutorial I’ll show you how to host a static website with HTTPS on AWS wit…

leetcode 387. 字符串中的第一个唯一字符(hash)

给定一个字符串&#xff0c;找到它的第一个不重复的字符&#xff0c;并返回它的索引。如果不存在&#xff0c;则返回 -1。 示例&#xff1a; s “leetcode” 返回 0 s “loveleetcode” 返回 2 class Solution { public int firstUniqChar(String s) { int[][] tempnew i…

marlin 三角洲_三角洲湖泊和数据湖泊-入门

marlin 三角洲Data lakes are becoming adopted in more and more companies seeking for efficient storage of their assets. The theory behind it is quite simple, in contrast to the industry standard data warehouse. To conclude this this post explains the logica…

tomcat中设置Java 客户端程序的http(https)访问代理

1、假定http/https代理服务器为 127.0.0.1 端口为8118 2、在tomcat/bin/catalina.sh脚本文件中设置JAVA_OPTS&#xff0c;如下图&#xff1a; 保存后重启tomcat就能生效。转载于:https://www.cnblogs.com/zhangmingcheng/p/11211776.html

java界面中显示图片_java中怎样在界面中显示图片?

方法一&#xff1a;JLabel helloLabel new JLabel("New label");helloLabel.setIcon(new ImageIcon("E:\\javaSE\u4EE3\u7801\\TimeManager\\asset\\hello.gif"));helloLabel.setBackground(Color.BLACK);helloLabel.setBounds(0, 0, 105, 50);contentPan…

one-of-k 编码算法_我们如何教K-12学生如何编码

one-of-k 编码算法by Christopher George克里斯托弗乔治(Christopher George) 我们如何教K-12学生如何编码 (How we’re teaching K-12 students how to code) Hello World! (Sorry, I couldn’t resist.) My name is Christopher George and I am currently a Junior at Carn…

knime简介_KNIME简介

knime简介Data Science is abounding. It considers different realms of the data world including its preparation, cleaning, modeling, and whatnot. To be precise, it is massive in terms of the span it covers and the opportunities it offers. Needless to say, th…

hadoop2.x HDFS快照介绍

说明&#xff1a;由于近期正好在研究hadoop的快照机制。看官网上的文档讲的非常仔细。就顺手翻译了。也没有去深究一些名词的标准译法&#xff0c;所以可能有些翻译和使用方法不是非常正确&#xff0c;莫要介意~~ 原文地址&#xff1a;&#xff08;Apache hadoop的官方文档&…

MQTT服务器搭建--Mosquitto用户名密码配置

前言&#xff1a; 基于Mosquitto服务器已经搭建成功&#xff0c;大部分都是采用默认的是允许匿名用户登录模式&#xff0c;正式上线的系统需要进行用户认证。 1.用户参数说明 Mosquitto服务器的配置文件为/etc/mosquitto/mosquitto.conf&#xff0c;关于用户认证的方式和读取的…

java number string_java基础系列(一):Number,Character和String类及操作

这篇文章总结了Java中最基础的类以及常用的方法&#xff0c;主要有&#xff1a;Number&#xff0c;Character&#xff0c;String。1、Number类在实际开发的过程中&#xff0c;常常会用到需要使用对象而不是内置的数据类型的情形。所以&#xff0c;java语言为每个内置数据类型都…

谁参加了JavaScript 2018状况调查?

by Sacha Greif由Sacha Greif 谁参加了JavaScript 2018状况调查&#xff1f; (Who Took the State of JavaScript 2018 Survey?) 我们如何努力使调查更具代表性 (How we’re working to make the survey more representative) I was recently listening to a podcast episode…

机器学习 建立模型_建立生产的机器学习系统

机器学习 建立模型When businesses plan to start incorporating machine learning to enhance their solutions, they more often than not think that it is mostly about algorithms and analytics. Most of the blogs/training on the matter also only talk about taking …

CDH使用秘籍(一):Cloudera Manager和Managed Service的数据库

背景从业务发展需求&#xff0c;大数据平台须要使用spark作为机器学习、数据挖掘、实时计算等工作&#xff0c;所以决定使用Cloudera Manager5.2.0版本号和CDH5。曾经搭建过Cloudera Manager4.8.2和CDH4&#xff0c;在搭建Cloudera Manager5.2.0版本号的时候&#xff0c;发现对…

leetcode 455. 分发饼干(贪心算法)

假设你是一位很棒的家长&#xff0c;想要给你的孩子们一些小饼干。但是&#xff0c;每个孩子最多只能给一块饼干。 对每个孩子 i&#xff0c;都有一个胃口值 g[i]&#xff0c;这是能让孩子们满足胃口的饼干的最小尺寸&#xff1b;并且每块饼干 j&#xff0c;都有一个尺寸 s[j]…