当前位置:网站首页>Create binary tree
Create binary tree
2022-04-23 06:00:00 【hanyc..】
package binaryTree;
import java.util.*;
// Define the nodes of a binary tree
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(" Enter the node value of the tree ( Input -1 Then terminate ):");
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(" Middle order traversal result :");
System.out.println(tree);
}
//( Recursive in order ) Create a binary 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;
}
// Non recursive middle order traversal
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;
}
}
Test cases :


版权声明
本文为[hanyc..]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230541160421.html
边栏推荐
- 线性代数第一章-行列式
- PyQy5学习(二):QMainWindow+QWidget+QLabel
- RedHat6之smb服务访问速度慢解决办法记录
- Pytorch learning record (XI): data enhancement, torchvision Explanation of various functions of transforms
- Implementation of displaying database pictures to browser tables based on thymeleaf
- MySql基础狂神说
- Shansi Valley P290 polymorphism exercise
- Pytorch学习记录(五):反向传播+基于梯度的优化器(SGD,Adagrad,RMSporp,Adam)
- Pytorch——数据加载和处理
- In depth understanding of the relationship between dncblevel and noise denoising in the paper
猜你喜欢

Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison

Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code

深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索

sklearn之 Gaussian Processes

Pytoch learning record (x): data preprocessing + batch normalization (BN)

Pyqy5 learning (4): qabstractbutton + qradiobutton + qcheckbox

String notes

Font shape `OMX/cmex/m/n‘ in size <10.53937> not available (Font) size <10.95> substituted.

Pytoch -- data loading and processing

Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
随机推荐
rsync实现文件服务器备份
Filebrowser realizes private network disk
The official website of UMI yarn create @ umijs / UMI app reports an error: the syntax of file name, directory name or volume label is incorrect
Pytorch学习记录(七):处理数据和训练模型的技巧
线性代数第一章-行列式
Shansi Valley P290 polymorphism exercise
Pytorch學習記錄(十三):循環神經網絡((Recurrent Neural Network)
Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决
Ptorch learning record (XIII): recurrent neural network
你不能访问此共享文件夹,因为你组织的安全策略阻止未经身份验证的来宾访问
JVM family (4) -- memory overflow (OOM)
Multithreading and high concurrency (1) -- basic knowledge of threads (implementation, common methods, state)
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
Software architecture design - software architecture style
图解HashCode存在的意义
去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
JDBC工具类封装
治疗TensorFlow后遗症——简单例子记录torch.utils.data.dataset.Dataset重写时的图片维度问题
Pytorch learning record (IX): convolutional neural network in pytorch
Gaussian processes of sklearn