mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 23:56:28 +08:00
ec82be7dc2
* fix: fixed the bug of arrToTree function * feat(tree): Add the array representation of the binary tree(js,ts) * refactor: Make the arrToTree method match the method in java
41 lines
940 B
JavaScript
41 lines
940 B
JavaScript
/**
|
|
* File: TreeNode.js
|
|
* Created Time: 2022-12-04
|
|
* Author: IsChristina (christinaxia77@foxmail.com)
|
|
*/
|
|
|
|
/**
|
|
* Definition for a binary tree node.
|
|
*/
|
|
class TreeNode {
|
|
val; // 节点值
|
|
left; // 左子节点指针
|
|
right; // 右子节点指针
|
|
height; //节点高度
|
|
constructor(val, left, right, height) {
|
|
this.val = val === undefined ? 0 : val;
|
|
this.left = left === undefined ? null : left;
|
|
this.right = right === undefined ? null : right;
|
|
this.height = height === undefined ? 0 : height;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Generate a binary tree given an array
|
|
* @param arr
|
|
* @return
|
|
*/
|
|
function arrToTree(arr, i = 0) {
|
|
if (i < 0 || i >= arr.length || arr[i] === null) {
|
|
return null;
|
|
}
|
|
let root = new TreeNode(arr[i]);
|
|
root.left = arrToTree(arr, 2 * i + 1);
|
|
root.right = arrToTree(arr, 2 * i + 2);
|
|
return root;
|
|
}
|
|
|
|
module.exports = {
|
|
TreeNode,
|
|
arrToTree,
|
|
};
|