Files
hello-algo/ru/codes/javascript/modules/TreeNode.js
T
Yudong JinandClaude Opus 5.5 ded7ddc635 Revert the runnable-snippets batches to the #2033 state (#2038)
Reverts #2034, #2035, #2036 and #2037. The work will return in a single
reviewed PR that is completed before it merges. The tree equals 8c96e2f30
(#2033).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-16 06:45:32 +08:00

36 lines
1.0 KiB
JavaScript

/**
* File: TreeNode.js
* Created Time: 2022-12-04
* Author: IsChristina ([email protected])
*/
/* Узел двоичного дерева */
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;
}
}
/* Десериализовать массив в двоичное дерево */
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,
};