#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
#define error(format, ...) printf(format, __VA_ARGS__)
struct TreeNode
{
TreeNode *left;
TreeNode *right;
int val;
};
TreeNode *createBinaryTree(TreeNode *Arr, int size)
{
for (int i = 0; i <= size; i++)
{
(Arr + i)->val = i;
}
int gap = size / 2;
for (int i = 1; i <= gap; ++i)
{
(Arr + i)->left = (Arr + (i << 1));
(Arr + i)->right = i != gap ? (Arr + ((i << 1) + 1)) : (gap << 1) + 1 == size ? (Arr + size) : nullptr;
}
TreeNode *root = (Arr + 1);
return root;
}
void preTraverseTree(TreeNode *root)
{
if (root == nullptr)
{
return;
}
cout << root->val;
preTraverseTree(root->left);
preTraverseTree(root->right);
}
void PreTraverseTree(TreeNode *root)
{
stack<TreeNode *> S;
S.push(root);
while (!S.empty())
{
TreeNode *top = S.top();
cout << top->val;
S.pop();
if (top->right)
{
S.push(top->right);
}
if (top->left)
{
S.push(top->left);
}
}
}
void midTraverseTree(TreeNode *root)
{
if (root == nullptr)
{
return;
}
midTraverseTree(root->left);
cout << root->val;
midTraverseTree(root->right);
}
void MidTraverseTree(TreeNode *root)
{
stack<TreeNode *> stack;
while (root || !stack.empty())
{
while (root != nullptr)
{
stack.push(root);
root = root->left;
}
root = stack.top();
stack.pop();
cout << root->val;
root = root->right;
}
}
void backTraverseTree(TreeNode *root)
{
if (!root)
{
return;
}
backTraverseTree(root->left);
backTraverseTree(root->right);
cout << root->val;
}
void BackTraverseTree(TreeNode *root)
{
std::vector<int> v;
stack<TreeNode *> stack;
stack.push(root);
while (!stack.empty())
{
TreeNode *top = stack.top();
stack.pop();
v.push_back(top->val);
if (top->left)
{
stack.push(top->left);
}
if (top->right)
{
stack.push(top->right);
}
}
reverse(v.begin(), v.end());
for (int i : v)
{
cout << i;
}
}
void morris(TreeNode *root)
{
if (!root)
{
return;
}
TreeNode *cur = root;
TreeNode *mostright = nullptr;
while (cur)
{
mostright = cur->left;
if (mostright)
{
while (mostright->right && mostright->right != cur)
{
mostright = mostright->right;
}
if (!mostright->right)
{
mostright->right = cur;
cout << cur->val;
cur = cur->left;
continue;
}
else
{
mostright->right = nullptr;
}
}
else
{
cout << cur->val;
}
cur = cur->right;
}
}
vector<vector<int>> rankTraverse(TreeNode *root)
{
vector<vector<int>> V;
if (!root)
{
return V;
}
deque<TreeNode *> dq;
dq.push_back(root);
while (!dq.empty())
{
int count = dq.size();
vector<int> v;
while (count > 0)
{
TreeNode *head = dq.front();
dq.pop_front();
v.push_back(head->val);
if (head->left)
{
dq.push_back(head->left);
}
if (head->right)
{
dq.push_back(head->right);
}
count--;
}
V.push_back(v);
}
}
vector<vector<int>> zigzagLevelOrder(TreeNode *root)
{
vector<vector<int>> V;
if (!root)
return V;
deque<TreeNode *> dq;
bool flag = true;
dq.push_front(root);
while (!dq.empty())
{
int count = dq.size();
vector<int> v;
while (count > 0)
{
TreeNode *head;
if (flag)
{
head = dq.back();
dq.pop_back();
if (head->left)
dq.push_front(head->left);
if (head->right)
dq.push_front(head->right);
}
if (!flag)
{
head = dq.front();
dq.pop_front();
if (head->right)
dq.push_back(head->right);
if (head->left)
dq.push_back(head->left);
}
count--;
}
flag = !flag;
V.push_back(v);
}
return V;
}
void SwapTree(TreeNode *root)
{
if (!root)
return;
SwapTree(root->left);
SwapTree(root->right);
if (!root->left && root->right)
{
root->left = new TreeNode();
root->left->val = root->right->val;
root->right = nullptr;
}
else if (!root->right && root->left)
{
root->right = new TreeNode();
root->right->val = root->left->val;
root->left = nullptr;
}
else if (root->right && root->left)
{
swap(root->left->val, root->right->val);
}
return;
}
void flattern(TreeNode *root)
{
if (!root)
{
return;
}
TreeNode *cur = root;
TreeNode *rtemp = root;
TreeNode *ltemp = root;
while (cur->left || cur->right)
{
if (!cur->left)
{
cur = cur->right;
continue;
}
rtemp = cur->right;
ltemp = cur->left;
cur->right = ltemp;
cur->left = nullptr;
while (ltemp->right)
{
ltemp = ltemp->right;
}
ltemp->right = rtemp;
cur = cur->right;
}
return;
}
bool judge(TreeNode *root)
{
if (!root)
{
return false;
}
}
bool isValidBST(TreeNode *root)
{
if (!root)
{
return true;
}
bool a = isValidBST(root->right);
bool b = isValidBST(root->left);
return root->left->val < root->val && root->right->val > root->val;
}
void getparentNum(TreeNode *root, TreeNode *parent, int key)
{
if (!root)
{
return;
}
if (root->val == key)
{
if (root == parent)
{
return;
}
else
{
cout << "找到了" << endl;
cout << parent->val;
}
}
getparentNum(root->left, root, key);
getparentNum(root->right, root, key);
}
void getWPL(TreeNode *r, int depth, int &count)
{
if (!r)
{
return;
}
if (!r->left && !r->right)
{
count += (depth * r->val);
return;
}
getWPL(r->left, depth + 1, count);
getWPL(r->right, depth + 1, count);
}
int getSum(TreeNode* root){
if(root==nullptr){
return 0;
}
return root->val + getSum(root->left) + getSum(root->right);
}
int main(int argc, char const *argv[])
{
cout << "输入结点数" << endl;
int size = 0;
cin >> size;
TreeNode *Arr = new TreeNode[size + 1];
TreeNode *arr = createBinaryTree(Arr, size);
cout << "层次遍历" << endl;
vector<vector<int>> V = rankTraverse(arr);
for (int i = 0; i < V.size(); i++)
{
for (int j = 0; j < V.at(i).size(); j++)
{
cout << V.at(i).at(j);
}
cout << endl;
}
cout << endl;
cout << getSum(arr);
delete[] Arr;
return 0;
}