BinaryTree C++

最近刷LeetCode回顾二叉树相关知识,于是想自己先把二叉树的建立以及基本操作实现一遍。

#include
#include
#include
#include
using namespace std;
struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 };
TreeNode* InitTree(){
    int rootData;
    TreeNode* root = NULL;
    printf("请输入根节点数据: 0 为空节点\n");
    scanf("%d",&rootData);
    if(rootData == 0) {
        printf("创建一个空树");
        return NULL;
    }
    root = new TreeNode(rootData);
    //借助队列来创建节点
    queue q;
    q.push(root);
    while(q.empty() == false){
        int leftDate;
        int rightDate;
        TreeNode* t = q.front();
        q.pop();
        printf("请输入左子树节点:");
        scanf("%d",&leftDate);
        if(leftDate == 0){
            t->left = NULL;
        }else{
            TreeNode* leftNode = new TreeNode(leftDate); 
            t->left = leftNode;
            q.push(leftNode);
        }
        printf("请输入右子树节点:");
        scanf("%d",&rightDate);
        if(rightDate == 0){
            t->right = NULL;
        }else{
            TreeNode* rightNode = new TreeNode(rightDate);
            t->right = rightNode;
            q.push(rightNode);
        }
        
    }   
    return root;
}

void printNodeByQ(TreeNode* root){
    if(root!=NULL){
        printf("%d ",root->val);
        printNodeByQ(root->left);
        printNodeByQ(root->right);
    }
}
void printNodeByZ(TreeNode* root){
    if(root!=NULL){
        printNodeByZ(root->left);
        printf("%d ",root->val);
        printNodeByZ(root->right);
    }
}

void printNodeByH(TreeNode* root){
    if(root!=NULL){
        printNodeByH(root->left);
        printNodeByH(root->right);
        printf("%d ",root->val);
    }
}

void printNodeByC(TreeNode* root){
    if(root == NULL){
        return;
    }
    queue q;
    q.push(root);
    while(q.empty()==false){
        TreeNode* t = q.front();
        q.pop();
        printf("%d ",t->val);
        if(t->left!=NULL){
            q.push(t->left);
        }
        if(t->right!=NULL){
            q.push(t->right);
        }        
    }
}

int getTreeHeight(TreeNode* root){
    int leftHeight,rightHeight;
    if(root == NULL) return 0;
    if(root!=NULL){
        leftHeight = getTreeHeight(root->left);
        rightHeight = getTreeHeight(root->right);
        int height = max(rightHeight,leftHeight)+1;
        return height;
    }
    
}

void getYeZiNode(TreeNode* root){
    if(root!=NULL){
        if(root->left == NULL && root->right == NULL){
            printf("%d ",root->val);
        }else{
            getYeZiNode(root->left);
            getYeZiNode(root->right);
        }    
    }
}

int getNodeNum(TreeNode* root){
    if(root == NULL){
        return 0;
    }
    return getNodeNum(root->left)+ getNodeNum(root->right)+1;
    
}

int main(){
    // 构造一个二叉树
    TreeNode* root = InitTree();
    if(root == NULL){
        printf("创建一颗根节点为空的树");
    }else{
        //遍历这棵二叉树
        printNodeByC(root);
        //求树高
        printf("\n树高:%d\n",getTreeHeight(root));
        //遍历叶子节点
        printf("遍历叶子节点:");
        getYeZiNode(root);
        //求总的节点个数
        printf("\n二叉树节点数:%d\n",getNodeNum(root));
        
    }
    
}


你可能感兴趣的:(ACM)