mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 23:50:45 +08:00
Batch 3b of the runnable-snippets plan. avl_tree_node now cites each language's shared TreeNode in all 13 languages, and the C tree_node_class shares the same region. Rust gets an import region; Java, C#, Swift and Kotlin skip their serialization helpers with an adjacent region. The shared nodes gain reviewed field comments in C++ and Rust, aligned comments in Java, Swift and Kotlin, and a spacing fix in JavaScript. The AVL section adds one sentence saying the shared node class already has height. Four ru Rust files whose multi-line statements had collapsed into literal \n now compile again. en, ja, ko and zh-hant are regenerated; ru is synced by hand. Co-Authored-By: Claude Opus 5.5 <[email protected]>
38 lines
1.1 KiB
JavaScript
38 lines
1.1 KiB
JavaScript
/**
|
|
* File: TreeNode.js
|
|
* Created Time: 2022-12-04
|
|
* Author: IsChristina ([email protected])
|
|
*/
|
|
|
|
// [region]{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;
|
|
}
|
|
}
|
|
// [endregion]{tree_node}
|
|
|
|
/* Десериализовать массив в двоичное дерево */
|
|
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,
|
|
};
|