当前位置:网站首页>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;
}
}
边栏推荐
- 浓眉大眼的谷歌 Chrome 也叛变了,教你一招快速清除其自带广告
- 安装gnome-screenshot截图工具
- &#x开头的是什么编码?
- 开放麒麟 openKylin 自动化开发者平台正式发布
- [ 动词词组 ] 合集
- js department budget and expenditure radar chart
- 业务-(课程-章节-小节)+课程发布一些业务思路
- Gradle series - Groovy overview, basic use (based on Groovy document 4.0.4) day2-1
- Binary tree search and backtracking problem (leetcode)
- Dart Log工具类
猜你喜欢
随机推荐
(selenium)Service geckodriver unexpectedly exited. Status code was: 64
Flink1.15 source code reading flink-clients - flink command line help command
js部门预算和支出雷达图
怎样修改MySQL数据库的密码
来n遍剑指--07. 重建二叉树
Module eight
Come n times - 06. Print the linked list from end to end
loadrunner-controller-手动场景Schedule配置
【软考软件评测师】2012综合知识历年真题
Qt 编译错误:C2228: “.key”的左边必须有类/结构/联合
Canvas particles change various shapes js special effects
NowCoderTOP17-22 二分查找/排序——持续更新ing
【TCP/IP】网络模型
qt pass custom structure parameters in different threads
Progressive Web App(PWA)
(C语言)程序环境和预处理
作为面试官,关于线程池的问题我一般这样套路...
[ 动词词组 ] 合集
&#x开头的是什么编码?
Kotlin—基本语法 (四)