当前位置:网站首页>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;
}
}
边栏推荐
猜你喜欢

如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南

Flink1.15 source code reading - PER_JOB vs APPLICATION execution process

GZIPInputStream 类源码分析

学习笔记——七周成为数据分析师《第二周:业务》:业务分析框架

浓眉大眼的谷歌 Chrome 也叛变了,教你一招快速清除其自带广告

js right dot single page scrolling introduction page

Are postgresql range queries faster than index queries?

djangoWeb应用框架+MySQL数据4

来n遍剑指--09. 用两个栈实现队列

如何判断自己是否适合IT行业?方法很简单
随机推荐
The future of the hybrid interface: conversational UI
Redis Cluster - Sentinel Mode Principle (Sentinel)
js空气质量aqi雷达图分析
postgresql 范围查询比索引查询快吗?
数据中台建设(六):数据体系建设
NowCoderTOP28-34二叉树——持续更新ing
Dart Log工具类
【节选】吴恩达给出的AI职业生涯规划
多个js雷达图同时显示
Kotlin—基本语法(一)
qt pass custom structure parameters in different threads
loadrunner-controller-手动场景Schedule配置
A Spark SQL online problem troubleshooting and positioning
matlab常用符号用法总结
数字加分隔符
js implements the 2020 New Year's Day countdown bulletin board
loadrunner-controller-目标场景Schedule配置
VMware下安装win10启动后进入Boot Manger界面如何解决
loadrunner-Controller负载测试-各模块功能记录01测试场景设计
Mybaits 常用问题详解