2023-01-06 03:39:19 +08:00
|
|
|
/**
|
2023-04-24 04:11:18 +08:00
|
|
|
* File: tree_node.hpp
|
2022-11-25 02:04:38 +08:00
|
|
|
* Created Time: 2021-12-19
|
|
|
|
* Author: Krahets (krahets@163.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-04-18 14:31:23 +08:00
|
|
|
#include <limits.h>
|
2023-07-19 16:09:27 +08:00
|
|
|
#include <vector>
|
2023-04-18 14:31:23 +08:00
|
|
|
|
2023-07-19 16:09:27 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
/* 二叉树节点结构体 */
|
2022-11-25 02:04:38 +08:00
|
|
|
struct TreeNode {
|
2022-12-03 17:17:58 +08:00
|
|
|
int val{};
|
2022-12-04 08:22:37 +08:00
|
|
|
int height = 0;
|
2022-12-03 17:17:58 +08:00
|
|
|
TreeNode *parent{};
|
|
|
|
TreeNode *left{};
|
|
|
|
TreeNode *right{};
|
|
|
|
TreeNode() = default;
|
2023-04-14 03:44:02 +08:00
|
|
|
explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent) {
|
|
|
|
}
|
2022-11-25 02:04:38 +08:00
|
|
|
};
|
|
|
|
|
2023-07-19 16:09:27 +08:00
|
|
|
// 序列化编码规则请参考:
|
|
|
|
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
|
|
|
// 二叉树的数组表示:
|
|
|
|
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
|
|
|
// 二叉树的链表表示:
|
|
|
|
// /——— 15
|
|
|
|
// /——— 7
|
|
|
|
// /——— 3
|
|
|
|
// | \——— 6
|
|
|
|
// | \——— 12
|
|
|
|
// ——— 1
|
|
|
|
// \——— 2
|
|
|
|
// | /——— 9
|
|
|
|
// \——— 4
|
|
|
|
// \——— 8
|
2022-12-01 09:09:34 +08:00
|
|
|
|
2023-07-19 16:09:27 +08:00
|
|
|
/* 将列表反序列化为二叉树:递归 */
|
|
|
|
TreeNode *vectorToTreeDFS(vector<int> &arr, int i) {
|
|
|
|
if (i < 0 || i >= arr.size() || arr[i] == INT_MAX) {
|
|
|
|
return nullptr;
|
2022-11-25 02:04:38 +08:00
|
|
|
}
|
2023-07-19 16:09:27 +08:00
|
|
|
TreeNode *root = new TreeNode(arr[i]);
|
|
|
|
root->left = vectorToTreeDFS(arr, 2 * i + 1);
|
|
|
|
root->right = vectorToTreeDFS(arr, 2 * i + 2);
|
2022-11-25 02:04:38 +08:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2023-07-19 16:09:27 +08:00
|
|
|
/* 将列表反序列化为二叉树 */
|
|
|
|
TreeNode *vectorToTree(vector<int> arr) {
|
|
|
|
return vectorToTreeDFS(arr, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 将二叉树序列化为列表:递归 */
|
|
|
|
void treeToVecorDFS(TreeNode *root, int i, vector<int> &res) {
|
2022-11-25 02:04:38 +08:00
|
|
|
if (root == nullptr)
|
2023-07-19 16:09:27 +08:00
|
|
|
return;
|
|
|
|
while (i >= res.size()) {
|
|
|
|
res.push_back(INT_MAX);
|
|
|
|
}
|
|
|
|
res[i] = root->val;
|
|
|
|
treeToVecorDFS(root->left, 2 * i + 1, res);
|
|
|
|
treeToVecorDFS(root->right, 2 * i + 2, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 将二叉树序列化为列表 */
|
|
|
|
vector<int> treeToVecor(TreeNode *root) {
|
|
|
|
vector<int> res;
|
|
|
|
treeToVecorDFS(root, 0, res);
|
|
|
|
return res;
|
2022-11-25 02:04:38 +08:00
|
|
|
}
|
2023-01-14 19:52:11 +08:00
|
|
|
|
2023-10-27 01:01:21 +08:00
|
|
|
/* 释放二叉树内存 */
|
2023-01-14 19:52:11 +08:00
|
|
|
void freeMemoryTree(TreeNode *root) {
|
2023-04-14 03:44:02 +08:00
|
|
|
if (root == nullptr)
|
|
|
|
return;
|
|
|
|
freeMemoryTree(root->left);
|
2023-01-14 19:52:11 +08:00
|
|
|
freeMemoryTree(root->right);
|
2023-04-14 03:44:02 +08:00
|
|
|
delete root;
|
2023-01-14 19:52:11 +08:00
|
|
|
}
|