当前位置:网站首页>JS构造二叉树
JS构造二叉树
2022-07-20 05:33:00 【一只哆啦呀】
方式1:
class Node {
// 定义节点
constructor(data){
this.data = data
this.leftChild = null
this.rightChild = null
}
}
const createTree = (arr) => {
// 创建二叉树
let tree = new Node(arr[0])
let Nodes = [tree]
let i = 1
for (let node of Nodes){
Nodes.push(node.leftChild = new Node (arr[i]))
i += 1
if (i == arr.length) return tree
Nodes.push(node.rightChild = new Node(arr[i]))
i += 1
if (i == arr.length) return tree
}
}
let datas = ['A','B','C','D','E','F','G',null,'H']
let t = createTree(datas)
方式2:
function TreeNode(val, left, right) {
this.val = (val===undefined ? 0 : val)
this.left = (left===undefined ? null : left)
this.right = (right===undefined ? null : right)
}
let biTree = new TreeNode(3);
biTree.left = new TreeNode(5);
biTree.right = new TreeNode(1);
biTree.left.left = new TreeNode(6);
biTree.left.right = new TreeNode(2);
biTree.left.right.left = new TreeNode(7);
biTree.left.right.right = new TreeNode(4);
biTree.right.left = new TreeNode(0);
biTree.right.right = new TreeNode(8);
方式3:
const bt = {
val: 1,
left: {
val: 2,
left: {
val: 4,
left: null,
right: null,
},
right: {
val: 5,
left: {
val: 8,
left: null,
right: null,
},
right: {
val: 9,
left: null,
right: null,
},
},
},
right: {
val: 3,
left: {
val: 6,
left: null,
right: null,
},
right: {
val: 7,
left: null,
right: null,
},
},
};
边栏推荐
- Analysis of KL divergence and cross entropy
- 直观理解Transpose Convolution
- Popular understanding of the principle of deep learning gradient accumulation
- 深度学习笔记——卷积层和池化层的一些“冷门”小知识点
- js方法封装汇总
- Swift archiving serialization
- 建筑空间温度分布预测模型与温度曲线图绘制毕业论文
- 城市规划设计毕业论文范文
- Intuitive understanding of diluted revolution
- 水文与水资源毕业论文题目【213个】
猜你喜欢
2D目标检测综述之检测模型篇(二)
TensorFlow v1 入门教程
[ROS robot system] autonomous navigation + Yolo target detection + voice broadcast
理解Seperable Convolution
基于STM32CubeMX的片外SPIFLASH嵌入FATFS
A popular understanding of the spectrum obtained by Fourier transform of images
STM32CubeMX通过FatFS读写U盘
Pytorch实现手写数字识别 | MNIST数据集(CNN卷积神经网络)
Paper study -- resource allocation and beamforming desing in the short blocklength region for urllc
Filter/split/sideoutput comparison of Flink diversion
随机推荐
中文自然语言处理相关资料 | Chinese NLP Toolkits 中文NLP工具
理解WGAN 和 Spectral Normalization(归一化)
2D目标检测综述之检测模型篇(二)
2D目标检测综述之神经网络篇
【CANN训练营】基于昇腾CANN平台的AI CPU算子开发
理解原始Gan Loss 和 Hinge Gan Loss
Li Hongyi 2020 machine learning -- p17 CNN & P 14
A popular understanding of the spectrum obtained by Fourier transform of images
Flink watermark
Tensorflow 1.x 和 Pytorch 中 Conv2d Padding的区别
Play with the one-stop plan of cann target detection and recognition [introduction]
Understanding the concept of differentiation and gradient
理解Seperable Convolution
RIoTBoard开发板系列笔记(八)—— 构建桌面系统
Tensorflow 1. The difference between conv2d padding in X and pytoch
关于图像傅里叶变换得到的频谱图的通俗理解
针对嵌入式设备外接设备的驱动开发心得
Play with the one-stop plan of cann target detection and recognition [basic]
[cann training camp] cann training camp_ Shengteng AI interesting application realizes AI interesting application (Part 1) essay
(6) Pytorch deep learning: logistic regression (multi-layer and multi-dimensional feature input)