当前位置:网站首页>NowCoderTOP23-27 Binary tree traversal - continuous update ing
NowCoderTOP23-27 Binary tree traversal - continuous update ing
2022-07-31 09:48:00 【Wang Hee Hee-】
TOP23. 二叉树的前序遍历
public class Solution {
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * @param root TreeNode类 * @return int整型一维数组 */
public void preorder(List<Integer> list, TreeNode root) {
// 空节点就返回
if(root == null) return;
// 前序遍历 ——> 根 左 右
list.add(root.val);
preorder(list, root.left);
preorder(list, root.right);
}
public int[] preorderTraversal (TreeNode root) {
// The final result is an array,so先定义一个数组
List<Integer> list = new ArrayList();
preorder(list, root);
// 定义一个数组长度为list大小
int[] res = new int[list.size()];
for(int i = 0; i < list.size(); i++) {
res[i] = list.get(i);
}
return res;
}
}
TOP24. 二叉树的中序遍历
public class Solution {
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * @param root TreeNode类 * @return int整型一维数组 */
public void inorder(List<Integer> list, TreeNode root) {
// 先判断树是否为空
if(root == null) return;
// 中序遍历 ——> 左 根 右
inorder(list, root.left);
list.add(root.val);
inorder(list, root.right);
}
public int[] inorderTraversal (TreeNode root) {
List<Integer> list = new ArrayList();
inorder(list, root);
// Define an array to store the results of in-order traversal
int[] res = new int[list.size()];
for(int i = 0; i < list.size(); i++) {
res[i] = list.get(i);
}
return res;
}
}
TOP25. 二叉树的后序遍历
public class Solution {
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * @param root TreeNode类 * @return int整型一维数组 */
public void postorder(List<Integer> list, TreeNode root) {
if(root == null) return;
postorder(list, root.left);
postorder(list, root.right);
list.add(root.val);
}
public int[] postorderTraversal (TreeNode root) {
List<Integer> list = new ArrayList();
postorder(list, root);
int[] res = new int[list.size()];
for(int i = 0; i < list.size(); i++) {
res[i] = list.get(i);
}
return res;
}
}
TOP26. 求二叉树的层序遍历
public class Solution {
/** * 使用队列实现层序遍历 * @param root TreeNode类 * @return int整型ArrayList<ArrayList<>> */
public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
if(root == null) return res;
// 队列存储,进行层次遍历
Queue<TreeNode> queue = new LinkedList<TreeNode>();
// 根节点先入队
queue.offer(root);
// 当队列不为空时
while(!queue.isEmpty()){
// 记录二叉树的某一行
ArrayList<Integer> row = new ArrayList<Integer>();
// The size of the queue is the number of elements in this layer
int n = queue.size();
// Start traversing all elements of this layer
for(int i = 0; i < n; i++) {
TreeNode cur = queue.poll();
row.add(cur.val);
//若是左右孩子存在,则存入左右孩子作为下一个层次
if(cur.left != null) queue.add(cur.left);
if(cur.right != null) queue.add(cur.right);
}
// Bring all nodes of a layer into the total result set
res.add(row);
}
return res;
}
}
TOP27. 按之字形顺序打印二叉树
public class Solution {
public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
if(pRoot == null) return res;
// 队列存储,进行层次遍历
Queue<TreeNode> queue = new LinkedList<TreeNode>();
// 根节点先入队
queue.offer(pRoot);
// 标记位,行数为偶数,逆置
Boolean flag = true;
// 当队列不为空时
while(!queue.isEmpty()){
flag = !flag;
// 记录二叉树的某一行
ArrayList<Integer> row = new ArrayList<Integer>();
// The size of the queue is the number of elements in this layer
int n = queue.size();
// Start traversing all elements of this layer
for(int i = 0; i < n; i++) {
TreeNode cur = queue.poll();
row.add(cur.val);
//若是左右孩子存在,则存入左右孩子作为下一个层次
if(cur.left != null) queue.add(cur.left);
if(cur.right != null) queue.add(cur.right);
}
if (flag)
{
Collections.reverse(row);
}
// Bring all nodes of a layer into the total result set
res.add(row);
}
return res;
}
}
边栏推荐
猜你喜欢

Gradle系列——Groovy概述,基础使用(基于Groovy文档4.0.4)day2-1

loadrunner脚本--添加事务

感情危机,朋友的网恋女友要和他闹分手,问我怎么办

centos7安装mysql5.7

Come n times with the sword--05. Replace spaces

Rich text editor Tinymce

【机器学习】用特征量重要度(feature importance)解释模型靠谱么?怎么才能算出更靠谱的重要度?

postgresql 范围查询比索引查询快吗?

第二十三课,抗锯齿(Anti Aliasing)

Emotional crisis, my friend's online dating girlfriend wants to break up with him, ask me what to do
随机推荐
Mysql+Navicat for Mysql
Open Kylin openKylin automation developer platform officially released
Qt 编译错误:C2228: “.key”的左边必须有类/结构/联合
乐观锁和悲观锁
Rich text editor Tinymce
【NLP】Transformer理论解读
Kotlin—基本语法(二)
win10镜像下载
MySQL 的几种碎片整理方案总结(解决delete大量数据后空间不释放的问题)
学习笔记——七周成为数据分析师《第二周:业务》:业务分析框架
&#x开头的是什么编码?
cocoaPods管理之后工程结构变化
Andoird开发--指南针(基于手机传感器)
ecshop安装的时候提示不支持JPEG格式
P5231 [JSOI2012]玄武密码(SAM 经典运用)
The big-eyed Google Chrome has also betrayed, teach you a trick to quickly clear its own ads
【微信小程序开发】生命周期与生命周期函数
通过栗子来学习MySQL高级知识点(学习,复习,面试都可)
MySQL 高级(进阶) SQL 语句 (一)
js部门预算和支出雷达图