二叉树常用操作集合
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <math.h>
using namespace std;
struct TreeNode{
int val;
TreeNode* left;
TreeNode* right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};
void preOrder(TreeNode* root){
if(!root) return;
cout << root->val << " ";
preOrder(root->left);
preOrder(root->right);
}
void inOrder(TreeNode* root){
if(!root) return;
inOrder(root->left);
cout << root->val << " ";
inOrder(root->right);
}
void postOrder(TreeNode* root){
if(!root) return;
postOrder(root->left);
postOrder(root->right);
cout << root->val << " ";
}
int calTreeDepth(TreeNode* root){
if(root == NULL) return 0;
else{
int m = calTreeDepth(root->left);
int n = calTreeDepth(root->right);
if(m > n) return m + 1;
else return n + 1;
}
}
int treeNodeCount(TreeNode* root){
if(root == NULL) return 0;
else return treeNodeCount(root->left) + treeNodeCount(root->right)+1;
}
int leafNodeCount(TreeNode* root){
if(root == NULL) return 0;
if(!root->left && !root->right){
return 1;
}else{
return leafNodeCount(root->left) + leafNodeCount(root->right);
}
}
int duCount(TreeNode* root){
if(root == NULL) return 0;
if((!root->left)&&(root->right) || (root->left)&&(root->right)){
return duCount(root->left) + duCount(root->right) + 1;
}
else{
return duCount(root->left) + duCount(root->right);
}
}
void printAllPath(TreeNode* root, char path[], int pathLen){
int i;
if(root != NULL){
path[pathLen] = root->val;
if(root->left == NULL && root->right == NULL);{
for(int i = pathLen; i >= 0; i--){
cout << path[i] << " ";
}
cout << endl;
}
}else{
printAllPath(root->left, path, pathLen + 1);
printAllPath(root->right, path, pathLen + 1);
}
}
void exchangeTreeNode(TreeNode* root){
TreeNode* temp;
if(root != NULL){
temp = root->left;
root->left = root->right;
root->right = temp;
exchangeTreeNode(root->left);
exchangeTreeNode(root->right);
}
}
void doubleOrderTraverse(TreeNode* root){
if(root != NULL){
cout << root->val;
doubleOrderTraverse(root->left);
cout << root->val;
doubleOrderTraverse(root->right);
}
}
void queueOrderTraverse(TreeNode* root){
if(root == NULL) return;
queue <TreeNode*> q;
q.push(root);
while(!q.empty()){
TreeNode* curr = q.front();
q.pop();
cout << curr->val << " ";
if(curr->left != NULL){
q.push(curr->right);
}
if(curr->right != NULL){
q.push(curr->right);
}
}
}
void stackTraverse(TreeNode* root){
if(root == NULL) return;
stack<TreeNode*> stc;
stc.push(root);
TreeNode* curr = root;
while(!stc.empty()){
curr = stc.top();
stc.pop();
cout << curr->val << " ";
if(curr->left){
stc.push(curr->left);
}
if(curr->right){
stc.push(curr->right);
}
}
}
TreeNode* treeInit(){
TreeNode* root = new TreeNode(2);
root->left = new TreeNode(1);
root->left->left = new TreeNode(0);
root->left->right = new TreeNode(4);
root->right = new TreeNode(3);
root->right->left = new TreeNode(2);
root->right->right = new TreeNode(5);
return root;
}
int main(){
TreeNode* root= treeInit();
cout << "preOrder traversal: ";
preOrder(root);
cout << endl;
cout << "inOrder traversal: ";
inOrder(root);
cout << endl;
cout << "postOrder traversal: ";
postOrder(root);
cout << endl;
cout << "Queue Traverse TreeNode: ";
queueOrderTraverse(root);
cout << endl;
cout << "Stack Traverse TreeNode: ";
stackTraverse(root);
cout << endl;
return 0;
}