2023-07-19 01:19:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: build_tree.cs
|
|
|
|
|
* Created Time: 2023-07-18
|
|
|
|
|
* Author: hpstory (hpstory1024@163.com)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace hello_algo.chapter_divide_and_conquer;
|
|
|
|
|
|
|
|
|
|
public class build_tree {
|
|
|
|
|
/* 构建二叉树:分治 */
|
2023-09-15 02:08:09 +08:00
|
|
|
|
public TreeNode dfs(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {
|
2023-07-19 01:19:49 +08:00
|
|
|
|
// 子树区间为空时终止
|
|
|
|
|
if (r - l < 0)
|
|
|
|
|
return null;
|
|
|
|
|
// 初始化根节点
|
|
|
|
|
TreeNode root = new TreeNode(preorder[i]);
|
|
|
|
|
// 查询 m ,从而划分左右子树
|
2023-09-15 02:08:09 +08:00
|
|
|
|
int m = inorderMap[preorder[i]];
|
2023-07-19 01:19:49 +08:00
|
|
|
|
// 子问题:构建左子树
|
2023-09-15 02:08:09 +08:00
|
|
|
|
root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);
|
2023-07-19 01:19:49 +08:00
|
|
|
|
// 子问题:构建右子树
|
2023-09-15 02:08:09 +08:00
|
|
|
|
root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);
|
2023-07-19 01:19:49 +08:00
|
|
|
|
// 返回根节点
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 构建二叉树 */
|
|
|
|
|
public TreeNode buildTree(int[] preorder, int[] inorder) {
|
|
|
|
|
// 初始化哈希表,存储 inorder 元素到索引的映射
|
2023-09-15 02:08:09 +08:00
|
|
|
|
Dictionary<int, int> inorderMap = new Dictionary<int, int>();
|
2023-07-19 01:19:49 +08:00
|
|
|
|
for (int i = 0; i < inorder.Length; i++) {
|
2023-09-15 02:08:09 +08:00
|
|
|
|
inorderMap.TryAdd(inorder[i], i);
|
2023-07-19 01:19:49 +08:00
|
|
|
|
}
|
2023-09-15 02:08:09 +08:00
|
|
|
|
TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.Length - 1);
|
2023-07-19 01:19:49 +08:00
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Test() {
|
|
|
|
|
int[] preorder = { 3, 9, 2, 1, 7 };
|
|
|
|
|
int[] inorder = { 9, 3, 1, 2, 7 };
|
|
|
|
|
Console.WriteLine("前序遍历 = " + string.Join(", ", preorder));
|
|
|
|
|
Console.WriteLine("中序遍历 = " + string.Join(", ", inorder));
|
|
|
|
|
|
|
|
|
|
TreeNode root = buildTree(preorder, inorder);
|
|
|
|
|
Console.WriteLine("构建的二叉树为:");
|
|
|
|
|
PrintUtil.PrintTree(root);
|
|
|
|
|
}
|
|
|
|
|
}
|