当前位置:网站首页>创建二叉树
创建二叉树
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
边栏推荐
- Add days to date
- freemark中插入图片
- Golang implements Ping connectivity detection case through exec module
- Hotkeys, interface visualization configuration (interface interaction)
- Object to map
- Strategy for improving the conversion rate of independent stations | recovering abandoned users
- xxl-job采坑指南xxl-rpc remoting error(connect timed out)
- mysql sql优化之Explain
- [machine learning] scikit learn introduction
- mysql如何将存储的秒转换为日期
猜你喜欢

多线程与高并发(2)——synchronized用法详解

The 8th Blue Bridge Cup 2017 - frog jumping cup

SQL语句简单优化

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

Pytorch deep learning practice_ 11 convolutional neural network

Hongji | how does HR carry out self change and organizational change in the digital era?

2 - principes de conception de logiciels

Ora: 28547 connection to server failed probable Oracle net admin error

Data mining -- understanding data

Flutter nouvelle génération de rendu graphique Impeller
随机推荐
Pavlov and hobbies
C, class library
Relative reference and absolute reference of Excel
Flutter nouvelle génération de rendu graphique Impeller
Some pits used by uni
引航成长·匠心赋能——YonMaster开发者培训领航计划全面开启
lambda表达式
Error 2003 (HY000) when Windows connects MySQL: can't connect to MySQL server on 'localhost' (10061)
Hongji | how does HR carry out self change and organizational change in the digital era?
Typescript interface & type rough understanding
2 - principes de conception de logiciels
Jiugong magic square - the 8th Lanqiao provincial competition - group C (DFS and comparison of all magic square types)
2-軟件設計原則
Golang implements Ping connectivity detection case through exec module
多线程与高并发(2)——synchronized用法详解
MySQL query uses \ g, column to row
2 - software design principles
xxl-job采坑指南xxl-rpc remoting error(connect timed out)
SQL语句简单优化
Fletter next generation graphics renderer impaller