mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-03 19:52:01 +08:00
Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
This commit is contained in:
@@ -10,24 +10,24 @@ import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class build_tree {
|
||||
/* 二分木を構築:分割統治 */
|
||||
/* 二分木の構築:分割統治法 */
|
||||
static TreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {
|
||||
// 部分木区間が空なら終了する
|
||||
// 部分木の区間が空の場合、終了する
|
||||
if (r - l < 0)
|
||||
return null;
|
||||
// ルートノードを初期化する
|
||||
// 根ノードを初期化する
|
||||
TreeNode root = new TreeNode(preorder[i]);
|
||||
// m を求めて左右部分木を分割する
|
||||
// m を調べ、左右の部分木に分割する
|
||||
int m = inorderMap.get(preorder[i]);
|
||||
// 部分問題:左部分木を構築する
|
||||
// 部分問題:左部分木の構築
|
||||
root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
|
||||
// 部分問題:右部分木を構築する
|
||||
// 部分問題:右部分木の構築
|
||||
root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
|
||||
// 根ノードを返す
|
||||
return root;
|
||||
}
|
||||
|
||||
/* 二分木を構築 */
|
||||
/* 二分木を構築する */
|
||||
static TreeNode buildTree(int[] preorder, int[] inorder) {
|
||||
// inorder の要素からインデックスへの対応を格納するハッシュテーブルを初期化する
|
||||
Map<Integer, Integer> inorderMap = new HashMap<>();
|
||||
@@ -41,11 +41,11 @@ public class build_tree {
|
||||
public static void main(String[] args) {
|
||||
int[] preorder = { 3, 9, 2, 1, 7 };
|
||||
int[] inorder = { 9, 3, 1, 2, 7 };
|
||||
System.out.println("前順走査 = " + Arrays.toString(preorder));
|
||||
System.out.println("中順走査 = " + Arrays.toString(inorder));
|
||||
System.out.println("行きがけ順走査 = " + Arrays.toString(preorder));
|
||||
System.out.println("通りがけ順走査 = " + Arrays.toString(inorder));
|
||||
|
||||
TreeNode root = buildTree(preorder, inorder);
|
||||
System.out.println("構築した二分木は:");
|
||||
System.out.println("構築した二分木:");
|
||||
PrintUtil.printTree(root);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user