当前位置:网站首页>创建二叉树
创建二叉树
2022-04-23 05:42:00 【hanyc..】
package binaryTree;
import java.util.*;
//定义二叉树的结点
class TreeNode {
int val;
TreeNode left;
TreeNode right;
public TreeNode(){
}
public TreeNode(int val){
this.val = val;
}
public TreeNode(int val, TreeNode left, TreeNode right){
this.val = val;
this.left = left;
this.right = right;
}
}
public class CreatTree {
public static void main(String[] args) {
System.out.println("输入树的节点值(输入-1则终止):");
Scanner sc = new Scanner(System.in);
LinkedList<Integer> list = new LinkedList<>();
while(true){
int temp = sc.nextInt();
if(temp == -1){
break;
}else{
list.addLast(temp);
}
}
TreeNode root = create(list);
List<Integer> tree = traverse(root);
System.out.print("中序遍历结果:");
System.out.println(tree);
}
//(按先序递归)创建二叉树
public static TreeNode create(LinkedList<Integer> list){
if(list==null||list.size()==0){
return null;
}
TreeNode node = null;
int data = list.removeFirst();
if(data!=0){
node = new TreeNode(data);
node.left = create(list);
node.right = create(list);
}
return node;
}
//非递归中序遍历
public static List<Integer> traverse(TreeNode root){
List<Integer> list = new ArrayList<>();
if(root == null){
return list;
}
Deque<TreeNode> stack = new LinkedList<>();
TreeNode cur = root;
while(cur!=null||!stack.isEmpty()){
if(cur!=null){
stack.push(cur);
cur = cur.left;
}else{
cur = stack.pop();
list.add(cur.val);
cur = cur.right;
}
}
return list;
}
}
测试案例:


版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42732184/article/details/123787945
边栏推荐
- MySQL创建oracle练习表
- Idea plug-in --- playing songs in the background
- io. lettuce. core. RedisCommandExecutionException: ERR wrong number of arguments for ‘auth‘ command
- Use of qwbengneview and qwebchannel.
- No.1.#_ 6 Navicat shortcuts
- MySQL realizes master-slave replication / master-slave synchronization
- Record a project experience and technologies encountered in the project
- Utf8 to STD: string and STD: string to utf8
- Split and merge multiple one-dimensional arrays into two-dimensional arrays
- jdbc入门\获取数据库连接\使用PreparedStatement
猜你喜欢

Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)

Batch import of orange single micro service

Flutter nouvelle génération de rendu graphique Impeller

手动删除eureka上已经注册的服务

Fletter next generation graphics renderer impaller

2-软件设计原则

Hongji cyclone RPA provides technical support for Guojin securities and realizes process automation in more than 200 business scenarios

Package mall system based on SSM

Deep learning object detection

opensips(1)——安装opensips详细流程
随机推荐
Hongji | how does HR carry out self change and organizational change in the digital era?
Flutter 新一代圖形渲染器 Impeller
Idea plug-in --- playing songs in the background
Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)
Total score of [Huawei machine test] (how to deal with the wrong answer? Go back once to represent one wrong answer)
Parameter analysis of open3d material setting
AcWing 836. Merge set (merge set)
The list attribute in the entity is empty or null, and is set to an empty array
多个一维数组拆分合并为二维数组
mysql中duplicate key update
创建线程的三种方式
Excel sets row and column colors according to cell contents
QT compressed folder
Ora: 28547 connection to server failed probable Oracle net admin error
7-10 longest symmetric substring (25 points) (violence problem solution) C language
lambda表达式
50 SQL exercises, answers and detailed analysis
转置卷积(Transposed Convolution)
refused connection
Record a project experience and technologies encountered in the project