mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-23 01:10:06 +08:00
Format C++ codes in Clang-Format Style: Microsoft
This commit is contained in:
@ -6,10 +6,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @brief Definition for a binary tree node
|
||||
*
|
||||
*/
|
||||
/* Definition for a binary tree node */
|
||||
struct TreeNode {
|
||||
int val{};
|
||||
int height = 0;
|
||||
@ -17,15 +14,11 @@ struct TreeNode {
|
||||
TreeNode *left{};
|
||||
TreeNode *right{};
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent) {}
|
||||
explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Generate a binary tree with a vector
|
||||
*
|
||||
* @param list
|
||||
* @return TreeNode*
|
||||
*/
|
||||
/* Generate a binary tree with a vector */
|
||||
TreeNode *vecToTree(vector<int> list) {
|
||||
if (list.empty())
|
||||
return nullptr;
|
||||
@ -37,12 +30,14 @@ TreeNode *vecToTree(vector<int> list) {
|
||||
while (!que.empty()) {
|
||||
auto node = que.front();
|
||||
que.pop();
|
||||
if (++index >= n) break;
|
||||
if (++index >= n)
|
||||
break;
|
||||
if (index < n) {
|
||||
node->left = new TreeNode(list[index]);
|
||||
que.emplace(node->left);
|
||||
}
|
||||
if (++index >= n) break;
|
||||
if (++index >= n)
|
||||
break;
|
||||
if (index < n) {
|
||||
node->right = new TreeNode(list[index]);
|
||||
que.emplace(node->right);
|
||||
@ -52,13 +47,7 @@ TreeNode *vecToTree(vector<int> list) {
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get a tree node with specific value in a binary tree
|
||||
*
|
||||
* @param root
|
||||
* @param val
|
||||
* @return TreeNode*
|
||||
*/
|
||||
/* Get a tree node with specific value in a binary tree */
|
||||
TreeNode *getTreeNode(TreeNode *root, int val) {
|
||||
if (root == nullptr)
|
||||
return nullptr;
|
||||
@ -69,15 +58,12 @@ TreeNode *getTreeNode(TreeNode *root, int val) {
|
||||
return left != nullptr ? left : right;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Free the memory allocated to a tree
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
/* Free the memory allocated to a tree */
|
||||
void freeMemoryTree(TreeNode *root) {
|
||||
if (root == nullptr) return;
|
||||
freeMemoryTree(root->left);
|
||||
if (root == nullptr)
|
||||
return;
|
||||
freeMemoryTree(root->left);
|
||||
freeMemoryTree(root->right);
|
||||
// 释放内存
|
||||
delete root;
|
||||
delete root;
|
||||
}
|
||||
|
Reference in New Issue
Block a user