当前位置:网站首页>JS construct binary tree
JS construct binary tree
2022-07-25 02:54:00 【One is dozing】
The way 1:
class Node {
// Define the node
constructor(data){
this.data = data
this.leftChild = null
this.rightChild = null
}
}
const createTree = (arr) => {
// Create a binary tree
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)
The way 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);
The way 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,
},
},
};
边栏推荐
- Use unicloud cloud function to decode wechat motion steps in applet
- # CF #807 Div.2(A - D)
- After working for two months in the summer vacation, I understood three routing schemes of keepalived high availability
- Solve the error: could not find 'xxxtest‘
- Details of happens before rules
- Sum of "n" numbers of force deduction summary
- [circular array] JS method collection of circular array
- Rotating frame target detection mmrotate v0.3.1 training hrsc2016 data set (II)
- Routing policy interferes with routing
- C: wechat chat software instance (wpf+websocket+webapi+entityframework)
猜你喜欢

Flink's study notes

Use unicloud cloud function to decode wechat motion steps in applet

Object.defineproperty use

DLL load failed: the page file is too small to complete the operation

Permanently mount the image steps

Explanation of C language file operation function

Vulntarget vulnerability shooting range -vulntarget-b

JS written test question -- realize the flat function of array

Do you know about real-time 3D rendering? Real time rendering software and application scenarios are coming
![[pyGame practice] nostalgic classic - do you remember the name of this chess game for children? (must collect)](/img/b3/075ad2d555118272efede5a9969319.png)
[pyGame practice] nostalgic classic - do you remember the name of this chess game for children? (must collect)
随机推荐
JS foundation -- object static method
Use unicloud cloud function to decode wechat motion steps in applet
Ctfshow misc introduction
YouTube Download and (batch) Download
StrError and PERROR
On Calc optimization of calcite
Solve the error: could not find 'xxxtest‘
Error: tomee required to support ear/ejb deployment
Beginners must see the markdown User Guide
Keras load history H5 format model error: attributeerror 'STR' object has no attribute 'decode‘
Generator set work arrangement problem code
How to communicate with aliens
Use pytest + allure to show the chart results (3)
hello csdn
BGP introduction
The file in scanpy1.9.1 cannot be read in scanpy1.7.2. The problem is solved
@Retryable @backoff @recover retry the use of annotations
Analysis of FLV packaging
Several dpdk control frameworks
Digital commerce cloud fine chemical industry management platform integrated informatization solution