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-04-14 03:44:02 +08:00
|
|
|
/* Definition for a binary tree node */
|
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-04-14 03:44:02 +08:00
|
|
|
/* Generate a binary tree with a vector */
|
2022-12-01 09:09:34 +08:00
|
|
|
TreeNode *vecToTree(vector<int> list) {
|
2023-01-08 19:03:22 +08:00
|
|
|
if (list.empty())
|
2022-12-01 09:09:34 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
auto *root = new TreeNode(list[0]);
|
|
|
|
queue<TreeNode *> que;
|
2023-01-08 19:03:22 +08:00
|
|
|
que.emplace(root);
|
2023-04-18 14:31:23 +08:00
|
|
|
size_t n = list.size(), i = 0;
|
2023-01-08 19:03:22 +08:00
|
|
|
while (!que.empty()) {
|
2022-12-01 09:09:34 +08:00
|
|
|
auto node = que.front();
|
2022-11-25 02:04:38 +08:00
|
|
|
que.pop();
|
2023-04-18 14:31:23 +08:00
|
|
|
if (++i >= n)
|
2023-04-14 03:44:02 +08:00
|
|
|
break;
|
2023-04-18 14:31:23 +08:00
|
|
|
// INT_MAX represent null
|
|
|
|
if (list[i] != INT_MAX) {
|
|
|
|
node->left = new TreeNode(list[i]);
|
2022-11-25 02:04:38 +08:00
|
|
|
que.emplace(node->left);
|
|
|
|
}
|
2023-04-18 14:31:23 +08:00
|
|
|
if (++i >= n)
|
2023-04-14 03:44:02 +08:00
|
|
|
break;
|
2023-04-18 14:31:23 +08:00
|
|
|
if (list[i] != INT_MAX) {
|
|
|
|
node->right = new TreeNode(list[i]);
|
2022-11-25 02:04:38 +08:00
|
|
|
que.emplace(node->right);
|
|
|
|
}
|
|
|
|
}
|
2022-12-01 09:09:34 +08:00
|
|
|
|
2022-11-25 02:04:38 +08:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/* Get a tree node with specific value in a binary tree */
|
2022-12-01 09:09:34 +08:00
|
|
|
TreeNode *getTreeNode(TreeNode *root, int val) {
|
2022-11-25 02:04:38 +08:00
|
|
|
if (root == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
if (root->val == val)
|
|
|
|
return root;
|
|
|
|
TreeNode *left = getTreeNode(root->left, val);
|
|
|
|
TreeNode *right = getTreeNode(root->right, val);
|
|
|
|
return left != nullptr ? left : right;
|
|
|
|
}
|
2023-01-14 19:52:11 +08:00
|
|
|
|
2023-04-14 03:44:02 +08:00
|
|
|
/* Free the memory allocated to a tree */
|
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
|
|
|
}
|