Open masx200 opened 1 year ago
output.hpp
/*** Start of inlined file: bfsTravelsal.hpp ***/
// +build ignore
#pragma once
/*** Start of inlined file: TreeNode.hpp ***/
// +build ignore
#pragma once
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode()
: val(0)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x)
: val(x)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x)
, left(left)
, right(right)
{
// cout << "newTreeNode:" << this << endl;
}
};
// #endif //
/*** End of inlined file: TreeNode.hpp ***/
#include <queue>
#include <string>
using namespace std;
// https://github.com/uniform641/treeparse/blob/master/test.cpp
std::string bfsTravelsal(TreeNode* root)
{
std::string result = "[";
std::queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (!nodeQueue.empty()) {
TreeNode* node = nodeQueue.front();
nodeQueue.pop();
if (node == nullptr) {
result += "null,";
} else {
result += std::to_string(node->val) + ",";
nodeQueue.push(node->left);
nodeQueue.push(node->right);
}
}
if (result == std::string("[null,")) {
return "[]";
}
result[result.size() - 1] = ']';
return result;
}
/*** End of inlined file: bfsTravelsal.hpp ***/
/*** Start of inlined file: freeTreeNode.hpp ***/
// +build ignore
#pragma once
// #ifndef _serialize_Tree_Node_
// #define _serialize_Tree_Node_
/*** Start of inlined file: TreeNode.hpp ***/
// +build ignore
#pragma once
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode()
: val(0)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x)
: val(x)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x)
, left(left)
, right(right)
{
// cout << "newTreeNode:" << this << endl;
}
};
// #endif //
/*** End of inlined file: TreeNode.hpp ***/
#include <iostream>
#include <stdio.h>
#include <string>
#include <sstream>
using namespace std;
void freeTreeNode(TreeNode* root)
{
if (root == NULL) {
return;
}
freeTreeNode(root->left);
freeTreeNode(root->right);
delete root;
// cout << "freeTreeNode:" << root << endl;
return;
}
// #endif //
/*** End of inlined file: freeTreeNode.hpp ***/
/*** Start of inlined file: LeetCodeTreeNodeToString.hpp ***/
// +build ignore
#pragma once
#include <queue>
#include <string>
using namespace std;
/*** Start of inlined file: TreeNode.hpp ***/
// +build ignore
#pragma once
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode()
: val(0)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x)
: val(x)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x)
, left(left)
, right(right)
{
// cout << "newTreeNode:" << this << endl;
}
};
// #endif //
/*** End of inlined file: TreeNode.hpp ***/
/*** Start of inlined file: bfsTravelsal.hpp ***/
// +build ignore
#pragma once
/*** Start of inlined file: TreeNode.hpp ***/
// +build ignore
#pragma once
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode()
: val(0)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x)
: val(x)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x)
, left(left)
, right(right)
{
// cout << "newTreeNode:" << this << endl;
}
};
// #endif //
/*** End of inlined file: TreeNode.hpp ***/
#include <queue>
#include <string>
using namespace std;
// https://github.com/uniform641/treeparse/blob/master/test.cpp
std::string bfsTravelsal(TreeNode* root)
{
std::string result = "[";
std::queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (!nodeQueue.empty()) {
TreeNode* node = nodeQueue.front();
nodeQueue.pop();
if (node == nullptr) {
result += "null,";
} else {
result += std::to_string(node->val) + ",";
nodeQueue.push(node->left);
nodeQueue.push(node->right);
}
}
if (result == std::string("[null,")) {
return "[]";
}
result[result.size() - 1] = ']';
return result;
}
/*** End of inlined file: bfsTravelsal.hpp ***/
#include <iostream>
#include <iterator>
#include <regex>
#include <sstream>
std::string LeetCodeTreeNodeToString(TreeNode* root)
{
stringstream sstream;
auto result = bfsTravelsal(root);
std::regex vowel_re("(,null){1,}\\]$");
std::regex_replace(std::ostreambuf_iterator<char>(sstream), result.begin(),
result.end(), vowel_re, "]");
return sstream.str();
}
/*** End of inlined file: LeetCodeTreeNodeToString.hpp ***/
/*** Start of inlined file: parseLeetCodeBinaryTree.hpp ***/
// +build ignore
#pragma once
#include <queue>
#include <string>
using namespace std;
/*** Start of inlined file: TreeNode.hpp ***/
// +build ignore
#pragma once
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode()
: val(0)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x)
: val(x)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x)
, left(left)
, right(right)
{
// cout << "newTreeNode:" << this << endl;
}
};
// #endif //
/*** End of inlined file: TreeNode.hpp ***/
/*** Start of inlined file: freeTreeNode.hpp ***/
// +build ignore
#pragma once
// #ifndef _serialize_Tree_Node_
// #define _serialize_Tree_Node_
/*** Start of inlined file: TreeNode.hpp ***/
// +build ignore
#pragma once
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode()
: val(0)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x)
: val(x)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x)
, left(left)
, right(right)
{
// cout << "newTreeNode:" << this << endl;
}
};
// #endif //
/*** End of inlined file: TreeNode.hpp ***/
#include <iostream>
#include <stdio.h>
#include <string>
#include <sstream>
using namespace std;
void freeTreeNode(TreeNode* root)
{
if (root == NULL) {
return;
}
freeTreeNode(root->left);
freeTreeNode(root->right);
delete root;
// cout << "freeTreeNode:" << root << endl;
return;
}
// #endif //
/*** End of inlined file: freeTreeNode.hpp ***/
// https://github.com/uniform641/treeparse/blob/master/treeParser.hpp
struct token {
bool isDigit;
int digit;
token(bool i, int d)
: isDigit(i)
, digit(d)
{
}
};
int tokenize(std::string& rawString, std::queue<token>& tokenQueue)
{
auto sign = 1;
int digit = 0;
bool digitFlag = false;
for (int i = 0; i < rawString.size(); i++) {
if (rawString[i] <= '9' && rawString[i] >= '0') {
digit = digit * 10 + rawString[i] - '0';
digitFlag = true;
} else if (rawString[i] == '-') {
sign = -1;
} else if (rawString[i] == ',') {
if (digitFlag)
tokenQueue.push(token { true, digit * sign });
digit = 0;
digitFlag = false;
sign = 1;
} else if (rawString[i] == ' ') {
continue;
} else if (rawString[i] == '[') {
continue;
} else if (rawString[i] == ']') {
if (digitFlag) {
tokenQueue.push(token { true, digit * sign });
}
continue;
} else if (rawString[i] == 'n' && static_cast<unsigned long long>(i) + 3 < rawString.size() && rawString[static_cast<std::basic_string<char, std::char_traits<char>, std::allocator<char>>::size_type>(i) + 1] == 'u' && rawString[static_cast<std::basic_string<char, std::char_traits<char>, std::allocator<char>>::size_type>(i) + 2] == 'l' && rawString[static_cast<std::basic_string<char, std::char_traits<char>, std::allocator<char>>::size_type>(i) + 2] == 'l') {
i += 3;
tokenQueue.push(token { false, 0 });
} else {
// unknown case
return -1;
}
}
return 0;
}
// void freeTreeNode(TreeNode* root)
// {
// if (root == nullptr) {
// return;
// }
// freeTreeNode(root->left);
// freeTreeNode(root->right);
// delete root;
// }
// parse LeetCode input to a Tree
// input format example: "[1,2,5,3,4,null,6]"
// return status: 0: success, -1: parse error, -2: tree structure invalid
// warning: this parser does not check the input format validity
int parseLeetCodeBinaryTree(std::string& rawString, TreeNode** pRoot)
{
*pRoot = nullptr;
std::queue<token> tokenQueue;
int status = tokenize(rawString, tokenQueue);
if (status != 0) {
return -1;
}
if (tokenQueue.empty() || !tokenQueue.front().isDigit) {
return 0;
}
std::queue<TreeNode*> nodeQueue;
int rootVal = tokenQueue.front().digit;
tokenQueue.pop();
*pRoot = new TreeNode(rootVal);
nodeQueue.push(*pRoot);
while (!nodeQueue.empty()) {
TreeNode* currentNode = nodeQueue.front();
nodeQueue.pop();
// get left child
if (!tokenQueue.empty()) {
token leftChild = tokenQueue.front();
tokenQueue.pop();
if (leftChild.isDigit) {
currentNode->left = new TreeNode(leftChild.digit);
nodeQueue.push(currentNode->left);
}
}
// get right child
if (!tokenQueue.empty()) {
token rightChild = tokenQueue.front();
tokenQueue.pop();
if (rightChild.isDigit) {
currentNode->right = new TreeNode(rightChild.digit);
nodeQueue.push(currentNode->right);
}
}
}
// shit happens
if (!tokenQueue.empty()) {
freeTreeNode(*pRoot);
return -2;
}
return 0;
}
/*** End of inlined file: parseLeetCodeBinaryTree.hpp ***/
/*** Start of inlined file: serializeTreeNode.hpp ***/
// +build ignore
#pragma once
// #ifndef _serialize_Tree_Node_
// #define _serialize_Tree_Node_
/*** Start of inlined file: TreeNode.hpp ***/
// +build ignore
#pragma once
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode()
: val(0)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x)
: val(x)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x)
, left(left)
, right(right)
{
// cout << "newTreeNode:" << this << endl;
}
};
// #endif //
/*** End of inlined file: TreeNode.hpp ***/
#include <iostream>
#include <stdio.h>
#include <string>
#include <sstream>
using namespace std;
string serializeTreeNode(TreeNode* root)
{
stringstream sstream;
if (root == NULL) {
sstream
<< "null";
return sstream.str();
}
sstream << "TreeNode"; //@"<<root;
sstream << "{val:" << root->val;
sstream << ",left:" << serializeTreeNode(root->left);
sstream << ",right:" << serializeTreeNode(root->right) << "}";
return sstream.str();
}
// #endif //
/*** End of inlined file: serializeTreeNode.hpp ***/
/*** Start of inlined file: traversalTreeNode.hpp ***/
// +build ignore
#pragma once
// #ifndef _serialize_Tree_Node_
// #define _serialize_Tree_Node_
/*** Start of inlined file: TreeNode.hpp ***/
// +build ignore
#pragma once
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode()
: val(0)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x)
: val(x)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x)
, left(left)
, right(right)
{
// cout << "newTreeNode:" << this << endl;
}
};
// #endif //
/*** End of inlined file: TreeNode.hpp ***/
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
void traversalTreeNode(TreeNode* root, vector<TreeNode*>& nodes)
{
if (root == NULL) {
return;
}
traversalTreeNode(root->left, nodes);
traversalTreeNode(root->right, nodes);
// delete root;
// cout << "freeTreeNode:" << root << endl;
nodes.emplace_back(root);
return;
}
// #endif //
/*** End of inlined file: traversalTreeNode.hpp ***/
/*** Start of inlined file: TreeNode.hpp ***/
// +build ignore
#pragma once
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode()
: val(0)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x)
: val(x)
, left(nullptr)
, right(nullptr)
{
// cout << "newTreeNode:" << this << endl;
}
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x)
, left(left)
, right(right)
{
// cout << "newTreeNode:" << this << endl;
}
};
// #endif //
/*** End of inlined file: TreeNode.hpp ***/
You can try using my forked project, which implements this #pragma once
feature.
https://github.com/rindeal/Amalgamate
Is the file duplicated inline?Why can't "#pragma once" be recognized correctly?
source code
https://gitee.com/masx200/leetcode-treenode-cpp/tree/0136bf26abba93f51c694dd97c5ce20f734b9e55/