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
991 B
JavaScript
38 lines
991 B
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,
|
|
};
|