From 2b0cf6f9ab2a2e448bb455158d842794829bbdef Mon Sep 17 00:00:00 2001 From: Justin Tse Date: Mon, 13 Nov 2023 14:06:17 +0800 Subject: [PATCH] fix: the bug for the array binary tree from ts and js code (#936) --- codes/javascript/chapter_tree/array_binary_tree.js | 2 +- codes/typescript/chapter_tree/array_binary_tree.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/codes/javascript/chapter_tree/array_binary_tree.js b/codes/javascript/chapter_tree/array_binary_tree.js index e5a5a600..92012503 100644 --- a/codes/javascript/chapter_tree/array_binary_tree.js +++ b/codes/javascript/chapter_tree/array_binary_tree.js @@ -40,7 +40,7 @@ class ArrayBinaryTree { /* 获取索引为 i 节点的父节点的索引 */ parent(i) { - return (i - 1) / 2; + return Math.floor((i - 1) / 2); // 向下取整 } /* 层序遍历 */ diff --git a/codes/typescript/chapter_tree/array_binary_tree.ts b/codes/typescript/chapter_tree/array_binary_tree.ts index 3687bc54..a32c3a72 100644 --- a/codes/typescript/chapter_tree/array_binary_tree.ts +++ b/codes/typescript/chapter_tree/array_binary_tree.ts @@ -42,7 +42,7 @@ class ArrayBinaryTree { /* 获取索引为 i 节点的父节点的索引 */ parent(i: number): number { - return (i - 1) / 2; + return Math.floor((i - 1) / 2); // 向下取整 } /* 层序遍历 */ @@ -148,4 +148,4 @@ console.log('中序遍历为:' + res); res = abt.postOrder(); console.log('后序遍历为:' + res); -export {}; +export { };