Binary Tree Level Order Traversal (leetcode 102/103/107)

news/2025/5/24 1:11:46

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

BFS

思路: 典型的BFS, 使用queue. 两层循环,外层判断queue 是否为空,内层for循环queue的size,然后逐个弹出node, 把左右儿子再压入。 注意开始的root == null 的判断。

时间复杂度: O(N)
空间复杂度: O(w) where w is maximum width of Binary Tree.


public class Solution {public List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> result = new ArrayList<List<Integer>>();Queue<TreeNode> queue = new LinkedList<TreeNode>();if (root == null) {return result;}queue.add(root);while (queue.peek() != null) {int size = queue.size();List<Integer> list = new ArrayList<Integer>();for (int i = 0; i < size; i++) {TreeNode node = queue.poll();list.add(node.val);if (node.left != null) {queue.add(node.left);}if (node.right != null) {queue.add(node.right);}}result.add(list);}return result;}
}

Binary Tree Level Order Traversal 2

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

思路: BFS, 因为是bottom-up, 所以每次加入list都加到开头:bfs.add(0, level);

public class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> bfs = new ArrayList<List<Integer>>();Queue<TreeNode> queue = new LinkedList<TreeNode>();if (root == null) {return bfs;}queue.add(root);while(!queue.isEmpty()){ArrayList<Integer> level = new ArrayList<Integer>();int size = queue.size();for(int i = 0; i < size; i++){TreeNode head = queue.poll();level.add(head.val);if(head.left != null){queue.add(head.left);}if(head.right != null){queue.add(head.right);}}bfs.add(0, level);}return bfs;}
}

Binary Tree Zigzag Level Order Traversal

思路: 设置一个flag,来确定该层是从左往右,还是从右往左。Collections.reverse(level) 变换方向。

public class Solution {public List<List<Integer>> zigzagLevelOrder(TreeNode root) {List<List<Integer>> result = new ArrayList<List<Integer>>();if(root == null){return result;}Queue<TreeNode> queue = new LinkedList<TreeNode>();queue.offer(root);int flag = -1;while(queue.peek() != null){flag = 0 - flag;int size = queue.size();List<Integer> level = new ArrayList<Integer>();for(int i = 0; i < size; i++){TreeNode head = queue.poll();level.add(head.val);if(head.left != null){queue.offer(head.left);}if(head.right != null){queue.offer(head.right);}}if(flag == -1){Collections.reverse(level);}result.add(level);}return result;}}

https://dhexx.cn/news/show-1219101.html

相关文章

centos7查看进程ps_Linux「第二节」-centos7.5部署influxdb

influxdb2.0还是alpha版本&#xff0c;离正式发布还需要一段时间&#xff0c;咱们就下载最新的1.7.9稳定版本&#xff0c;如下图&#xff1a;点上图的1.7.9按钮没有反应&#xff0c;感觉好奇怪&#xff1b;然后就用下面的地址来下载了&#xff1a;wget https://dl.influxdata.c…

postfix安装spamassassin clamav amavis

1.postfix搭建根据这个地址自己安装。点此进入2.yum安装&#xff0c;此安装包可以通过RPMForge软件仓库安装。# yum install -y amavisd-new clamav clamav-devel clamd spamassassin3.查看服务的开机自动启动状态# chkconfig --list | grep "amavisd\|clamd\|spamassass…

一行代码解决80%的错误

点击蓝字关注这个神奇的公众号&#xff5e;开发累了&#xff0c;轻松一下try {//10万行js代码//再也不怕bug } catch (e) {window.location.href "http://stackoverflow.com/search?q[js]" e.message;}

Docker和Kubernetes的Visual Studio Code扩展

“ 云原生 ”已成为最新一代软件开发技术和方法的总体术语。 这是关于使用Docker容器&#xff0c;Kubernetes和其他编排系统以及微服务编写在弹性&#xff0c;分布式环境中运行的软件。 新方法也需要新工具。 Microsoft的模块化&#xff0c;开放源代码编辑器和开发环境Visual …

【Java进阶】并发编程

1. 概述 三种性质 可见性&#xff1a;一个线程对共享变量的修改&#xff0c;另一个线程能立刻看到。缓存可导致可见性问题。原子性&#xff1a;一个或多个CPU执行操作不被中断。线程切换可导致原子性问题。有序性&#xff1a;编译器优化可能导致指令顺序发生改变。编译器优化可…

登陆安全过滤器

新建SecurityFilter.Java Java代码 package org.thj.bookstore.util; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servle…

python跨文件全局变量_Python 进程之间共享数据(全局变量)

进程之间共享数据(数值型)&#xff1a; import multiprocessing def func(num): num.value10.78 #子进程改变数值的值&#xff0c;主进程跟着改变 if __name__"__main__": nummultiprocessing.Value("d",10.0) # d表示数值,主进程与子进程共享这个value。&…

Java线上问题 | 大Json引发的血案

java线上问题分析大JSON引发的“血案”Java线上问题之日志打好与JVM参数配好这里想说的是打印日志的重要性&#xff0c;它在你定位问题时起到至关重要的作用。分析过程一、描述产生的问题服务无法访问&#xff0c;发现CPU被打满&#xff0c;同时进程僵死&#xff0c;jstack无法…

OpenStack Hacker养成指南

0 阅读指南 希望本文能够解开你心中萦绕已久的心结&#xff0c;假如是死结&#xff0c;请移步到 https://wiki.openstack.org/wiki/Main_Page学习OpenStack其实就是学习各种Python库的过程。把OpenStack的设计原则贴在你的墙上。 https://wiki.openstack.org/wiki/BasicDesignT…

Microsoft Ignite 2019开发人员的5大建议

微软的年度Ign​​ite会议通常迎合IT专业人员&#xff0c;重点是服务器&#xff0c;应用程序和管理工具。 今年情况并非如此&#xff0c;开发人员所占的份额更大。 真的&#xff0c;这并不奇怪。 微软到云巨头的过渡几乎已经完成&#xff0c;现代混合云需要可以利用Azure的超大…