当前位置:网站首页>二叉树的构建和遍历
二叉树的构建和遍历
2022-04-23 10:11:00 【Popuessing's Jersey】
import java.util.ArrayList;
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode root;
TreeNode(){}
TreeNode(int val){
this.val = val;
}
TreeNode(int val, TreeNode left,TreeNode right){
this.val = val;
this.right = right;
this.left = left;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
public TreeNode getRoot() {
return root;
}
public void setRoot(TreeNode root) {
this.root = root;
}
// 前序遍历——递归LC144——二叉树的前序遍历
ArrayList<Integer> preorderReverse(TreeNode root){
ArrayList<Integer> result = new ArrayList<>();
preOrder(root,result);
return result;
}
// 递归参数和返回类型
void preOrder(TreeNode root,ArrayList<Integer> result){
// 递归终止条件
if (root == null){
return;
}
// 递归单层逻辑
result.add(root.val);
preOrder(root.left,result);
preOrder(root.right,result);
}
// 中序遍历·递归·LC94_二叉树的中序遍历
public ArrayList<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> res = new ArrayList<>();
inorder(root,res);
return res;
}
void inorder(TreeNode root,ArrayList<Integer> list){
if (root == null){
return;
}
inorder(root.left,list);
list.add(root.val);
inorder(root.right,list);
}
// 后序遍历·递归·LC145_二叉树的后序遍历
public ArrayList<Integer> postorderTraversal(TreeNode root){
ArrayList <Integer> res = new ArrayList<>();
postorder(root,res);
return res;
}
void postorder(TreeNode root,ArrayList<Integer> list){
if (root == null){
return;
}
postorder(root.left,list);
postorder(root.right,list);
list.add(root.val);
}
public void createTree(int [] nums){
// 创建一个集合保存树的结点
ArrayList<TreeNode> treeNodes = new ArrayList<>();
for (int tempdata:nums) {
treeNodes.add(new TreeNode(tempdata));
}
// 将第一个节点设为根节点
root = treeNodes.get(0);
/*
* 利用构建完全二叉树的方式构建
* */
for (int i = 0; i < treeNodes.size()/2; i++) {
if ((i*2+1)< treeNodes.size()){
treeNodes.get(i).setLeft(treeNodes.get(i*2+1));
}
if ((i*2+2)< treeNodes.size()){
treeNodes.get(i).setRight(treeNodes.get(i*2+2));
}
}
}
public static void main(String[] args) {
int [] nums = {1,2,3,4,5,6,7,8};
TreeNode treeNode = new TreeNode();
treeNode.createTree(nums);
ArrayList<Integer> res1 =treeNode.preorderReverse(treeNode.getRoot());
ArrayList<Integer> res2 =treeNode.inorderTraversal(treeNode.getRoot());
ArrayList<Integer> res3 =treeNode.postorderTraversal(treeNode.getRoot());
System.out.println(res1);
System.out.println(res2);
System.out.println(res3);
}
}
输出结果:
[1, 2, 4, 8, 5, 3, 6, 7]
[8, 4, 2, 5, 1, 6, 3, 7]
[8, 4, 5, 2, 6, 7, 3, 1]
版权声明
本文为[Popuessing's Jersey]所创,转载请带上原文链接,感谢
https://blog.csdn.net/CoCo629vanilla/article/details/121600377
边栏推荐
- Construire neuf capacités de fabrication agile à l'ère métacosmique
- DBA common SQL statements (5) - latch related
- 构建元宇宙时代敏捷制造的九种能力
- Career planning and implementation in the era of meta universe
- Depth selector
- The central control learning infrared remote control module supports network and serial port control
- DBA常用SQL语句(4)- Top SQL
- ansible playbook语法和格式 自动化云计算
- Sim Api User Guide(5)
- 通过流式数据集成实现数据价值(4)-流数据管道
猜你喜欢

2022年制冷与空调设备运行操作考试练习题及模拟考试

《Redis设计与实现》
MapReduce核心和基础Demo

工业元宇宙平台规划与建设

2022年上海市安全员C证考试题库及答案

Custom login failure handling

101. Symmetric Tree

Interviewer: let's talk about some commonly used PHP functions. Fortunately, I saw this article before the interview

Arm debugging (1): two methods to redirect printf to serial port in keil

一文看懂 LSTM(Long Short-Term Memory)
随机推荐
CSP certification 202203-2 travel plan (multiple solutions)
[codeforces - 208e] blood cousins
Ansible cloud computing automation command line compact version
Chapter 3 enable and adjust the size of IM column storage (im-3.1)
Function realization of printing page
NEC infrared remote control coding description
第120章 SQL函数 ROUND
Sim Api User Guide(5)
2022年广东省安全员A证第三批(主要负责人)考试试题及答案
最长公共前串
Common DBA SQL statements (4) - Top SQL
Prefix sum of integral function -- Du Jiao sieve
Go语言实践模式 - 函数选项模式(Functional Options Pattern)
Examination questions and answers of the third batch (main person in charge) of Guangdong safety officer a certificate in 2022
Pyqt5 and communication
DBA常用SQL语句(3)- cache、undo、索引和等待事件
JVM——》常用命令
art-template 模板引擎
Common SQL statements of DBA (6) - daily management
2022年上海市安全员C证考试题库及答案