/** * File: tree_node.hpp * Created Time: 2021-12-19 * Author: krahets (krahets@163.com) */ #pragma once #include #include using namespace std; /* 二元樹節點結構體 */ struct TreeNode { int val{}; int height = 0; TreeNode *parent{}; TreeNode *left{}; TreeNode *right{}; TreeNode() = default; explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent) { } }; // 序列化編碼規則請參考: // 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 /* 將串列反序列化為二元樹:遞迴 */ TreeNode *vectorToTreeDFS(vector &arr, int i) { if (i < 0 || i >= arr.size() || arr[i] == INT_MAX) { return nullptr; } TreeNode *root = new TreeNode(arr[i]); root->left = vectorToTreeDFS(arr, 2 * i + 1); root->right = vectorToTreeDFS(arr, 2 * i + 2); return root; } /* 將串列反序列化為二元樹 */ TreeNode *vectorToTree(vector arr) { return vectorToTreeDFS(arr, 0); } /* 將二元樹序列化為串列:遞迴 */ void treeToVecorDFS(TreeNode *root, int i, vector &res) { if (root == nullptr) 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 treeToVecor(TreeNode *root) { vector res; treeToVecorDFS(root, 0, res); return res; } /* 釋放二元樹記憶體 */ void freeMemoryTree(TreeNode *root) { if (root == nullptr) return; freeMemoryTree(root->left); freeMemoryTree(root->right); delete root; }