#include <iostream>
#include<vector>
#include <stack>
#include <queue>
using namespace std;
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
/**
* 二叉树的先序遍历,中序遍历,后续遍历 非递归版本
*/
class TreeToSequence {
public:
vector<int> PreOrder(TreeNode *root){
vector<int>answer;
stack<TreeNode *>Q;
if(root==NULL){
return answer;
}
Q.push(root);
while(Q.size()){
TreeNode * cur=Q.top();
answer.push_back(cur->val);
Q.pop();
if(cur->right){
Q.push(cur->right);
}
if(cur->left){
Q.push(cur->left);
}
}
return answer;
}
vector<int> InOrder(TreeNode *root){
vector<int>answer;
stack<TreeNode *>Q;
TreeNode *cur=root;
while(Q.size() || cur){
if(cur){
Q.push(cur);
cur=cur->left;
}
else{
cur=Q.top();
answer.push_back(cur->val);
Q.pop();
cur=cur->right;
}
}
return answer;
}
vector<int> AfterOrder(TreeNode *root){
vector<int>answer;
stack<TreeNode *>Q;
if(root==NULL){
return answer;
}
Q.push(root);
TreeNode *h=root;
while(Q.size()){
TreeNode * cur=Q.top();
if(cur->left && cur->left!=h && cur->right!=h){
Q.push(cur->left);
}
else if(cur->right && cur->right!=h){
Q.push(cur->right);
}
else{
h=cur;
answer.push_back(h->val);
Q.pop();
}
}
return answer;
}
vector<vector<int> > convert(TreeNode* root) {
// write code here
vector<vector<int> >answer;
answer.push_back(PreOrder(root));
answer.push_back(InOrder(root));
answer.push_back(AfterOrder(root));
return answer;
}
};
/**
* 按层次输出二叉树
* 每一层占一行
* 用last 记录当前层的尾,nlast 记录下一层的尾,在层次遍历过程中,用新遍历的节点更新nlast,当队列当前的front 和 last相等时,就是要换行了,并且把last的值更新成 nlast 的值
*/
class TreePrinter {
public:
vector<vector<int> > printTree(TreeNode* root) {
// write code here
TreeNode *last=root;
TreeNode *nlast;
vector<vector<int> >answer;
queue<TreeNode *>Q;
Q.push(root);
vector<int>ans;
while(Q.size()){
TreeNode *cur=Q.front();
ans.push_back(cur->val);
Q.pop();
if(cur->left){
Q.push(cur->left);
nlast=cur->left;
}
if(cur->right){
nlast=cur->right;
Q.push(cur->right);
}
if(cur==last){
last=nlast;
answer.push_back(ans);
ans.clear();
}
}
return answer;
}
};
/**
* 把二叉树转换为string字符串保存,空节点用 "#!" 表示,每个节点值后面 加上"!" 来区分
*/
class TreeToString {
public:
void bfs(string &answer,TreeNode *root){
if(root==NULL){
answer+="#!";
return;
}
answer+=to_string(root->val);
answer+="!";
bfs(answer,root->left);
bfs(answer,root->right);
}
string toString(TreeNode* root) {
// write code here
string answer;
bfs(answer,root);
return answer;
}
};
/**
* 检查二叉树是否为平衡二叉树
* 即 检查每个节点的左右子树高度差是否超过 1
*
*
*/
class CheckBalance {
public:
int get_deep(TreeNode *root,bool &answer) {
if (root == NULL) {
return 0;
}
if (!answer) {
return -1;
}
int Ldeep = get_deep(root->left, answer) ;
int Rdeep = get_deep(root->right, answer);
int T = abs(Ldeep - Rdeep);
if (T > 1) {
answer = false;
}
return max(Ldeep, Rdeep) + 1;
}
bool check(TreeNode* root) {
bool answer=true;
if(root==NULL){
return answer;
}
int K=abs(get_deep(root->right,answer)-get_deep(root->left,answer));
if(K>1){
answer=false;
}
return answer;
}
};
int main() {
TreeNode *a=new TreeNode(1);
TreeNode *c=new TreeNode(3);
TreeNode *d=new TreeNode(4);
TreeNode *e=new TreeNode(5);
TreeNode *f=new TreeNode(6);
TreeNode *g=new TreeNode(7);
a->right=c,c->left=d;
vector<vector<int> >answer;
TreeToSequence A;
TreePrinter B;
TreeToString C;
CheckBalance D;
cout<<D.check(a)<<endl;
return 0;
}
二叉树先序遍历,中序遍历,后续遍历 非递归版本+二叉树转换为string保存+二叉树的层次遍历+平衡二叉树判断
最新推荐文章于 2022-09-02 16:08:04 发布