概述
#include <bits/stdc++.h>
using namespace std;
struct TreeNode
{
int val;
struct TreeNode* left;
struct TreeNode* right;
TreeNode(int x):val(x),left(nullptr),right(nullptr){}
};
//创建二叉树
TreeNode* createBiTree(vector<int>& TreeArray, int start)
{
if (TreeArray.empty()) return NULL;
if (TreeArray[start] == '#') return NULL;
int n = TreeArray.size();
TreeNode* root = new TreeNode(TreeArray[start]);
int lnode = start * 2 + 1;
int rnode = start * 2 + 2;
if (lnode > n - 1)
{
root->left = NULL;
}
else
{
root->left = createBiTree(TreeArray, lnode);
}
if (rnode > n - 1)
{
root->right = NULL;
}
else
{
root->right = createBiTree(TreeArray, rnode);
}
return root;
}
//先中后序遍历(递归法)
void traversal(TreeNode* cur, vector<int>& vec)
{
if (cur == NULL)
{
return;
}
//先序遍历
vec.push_back(cur->val);
traversal(cur->left, vec);
traversal(cur->right, vec);
//中序遍历
//traversal(cur->left, vec);
//vec.push_back(cur->val);
//traversal(cur->right, vec);
//后序遍历
//traversal(cur->left, vec);
//traversal(cur->right, vec);
//vec.push_back(cur->val);
}
//先序遍历(迭代法)
vector<int> preorderTraversal(TreeNode* root)
{
stack<TreeNode*> st;
vector<int> result;
if (root != NULL) st.push(root);
while (!st.empty())
{
TreeNode* node = st.top();
st.pop();
result.push_back(node->val);
if (node->right) st.push(node->right);
if (node->left) st.push(node->left);
}
return result;
}
//层序遍历(简单写法,返回一维数组)
vector<int> levelOrder(TreeNode* root)
{
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<int> result;
while (!que.empty())
{
TreeNode* node = que.front();
que.pop();
result.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
return result;
}
//层序遍历(返回二维数组)
vector<vector<int>> levelOrder2(TreeNode* root)
{
queue<TreeNode*> que;
if (root != NULL) que.push(root);
vector<vector<int>> result;
while (!que.empty())
{
int size = que.size();
vector<int> vec;
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
que.pop();
vec.push_back(node->val);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
result.push_back(vec);
}
return result;
}
//翻转二叉树(递归法)
void invertTree(TreeNode* root)
{
if (root == NULL) return;
swap(root->left, root->right);
invertTree(root->left);
invertTree(root->right);
}
//翻转二叉树(迭代法,深度优先)
void invertTree2(TreeNode* root)
{
if (root == NULL) return;
stack<TreeNode*> st;
st.push(root);
while (!st.empty())
{
TreeNode* node = st.top();
st.pop();
swap(node->left, node->right);
if (node->right) st.push(node->right);
if (node->left) st.push(node->left);
}
}
//翻转二叉树(迭代法,广度优先)
void invertTree3(TreeNode* root)
{
if (root == NULL) return;
queue<TreeNode*> que;
que.push(root);
while (!que.empty())
{
TreeNode* node = que.front();
que.pop();
swap(node->left, node->right);
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
//合并二叉树(递归法)
TreeNode* mergeTree(TreeNode* t1, TreeNode* t2)
{
if (t1 == NULL) return t2;
if (t2 == NULL) return t1;
TreeNode* root = new TreeNode(0);
root->val = t1->val + t2->val;
root->left = mergeTree(t1->left, t2->left);
root->right = mergeTree(t1->right, t2->right);
return root;
}
//判断是否是二叉搜索树
vector<int> vec;
void traversal(TreeNode* root) {
if (root == NULL) return;
traversal(root->left);
vec.push_back(root->val); // 将⼆叉搜索树转换为有序数组
traversal(root->right);
}
bool isValidBST(TreeNode* root) {
vec.clear(); // 不加这句在leetcode上也可以过,但最好加上
traversal(root);
for (int i = 1; i < vec.size(); i++) {
// 注意要⼩于等于,搜索树⾥不能有相同元素
if (vec[i] <= vec[i - 1]) return false;
}
return true;
}
int main(int argc, char** argv)
{
vector<int> invec = { 1,2,3,4,5,6,7};
TreeNode* root = createBiTree(invec, 0);
for (unsigned int i = 0; i < invec.size(); i++)
{
cout << invec[i] << " ";
}
vector<int> outvec;
//递归法先序遍历
traversal(root, outvec);
//迭代法先序遍历
outvec.clear();
outvec = preorderTraversal(root);
//层序遍历
outvec.clear();
outvec = levelOrder(root);
//翻转二叉树
outvec.clear();
invertTree(root);
invertTree2(root);
invertTree3(root);
outvec = levelOrder(root);
//判断二叉搜索树
cout << "root是否是二叉搜索树:"<< isValidBST(root) << endl;
for (unsigned int i = 0; i < outvec.size(); i++)
{
cout << outvec[i]<<" ";
}
system("pause");
return 0;
}
最后
以上就是淡定飞机为你收集整理的C++二叉树操作合集的全部内容,希望文章能够帮你解决C++二叉树操作合集所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复