经典面试题——二叉树

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;

struct Node {
    Node *l, *r;
    int x;
    Node(int x=0, Node *l=NULL, Node *r=NULL):x(x),l(l),r(r) {}    
};

class Tree {
    public:
        Tree(Node* root) {
            this->root = root;
        }
        
        Node* getRoot() {
            return root;
        }
        
        Node* buildTree() {
            int x;
            cin >> x;
            if(x != -1) {
                Node* curRoot = new Node(x);
                curRoot->l = buildTree();
                curRoot->r = buildTree();
                return curRoot;
            } else {
                return NULL;
            }
        }
        
        void print_pre_order(Node* cur) {
            if(cur == NULL)    return;
            printf("%d ", cur->x);
            print_pre_order(cur->l);
            print_pre_order(cur->r);
        }
        
        void print_in_order(Node* cur) {
            if(cur == NULL)    return;
            print_in_order(cur->l);
            printf("%d ", cur->x);
            print_in_order(cur->r);
        }
        
        void print_post_order(Node* cur) {
            if(cur == NULL)    return;
            print_post_order(cur->l);
            print_post_order(cur->r);
            printf("%d ", cur->x);
        }
        
        void print_level_order(Node* cur) {
            if(cur==NULL)    return;
            queue<Node*> que;
            que.push(cur);
            while(!que.empty()) {
                Node* c = que.front();
                que.pop();
                printf("%d ", c->x);
                if(c->l)    que.push(c->l);
                if(c->r)    que.push(c->r);
            }
        }
        
        int getNodeNum(Node* root) {
            if(root==NULL)    return 0;
            return getNodeNum(root->l) + getNodeNum(root->r) + 1;
        }
        
        int getLeafNum(Node* root) {
            if(root==NULL)    return 0;
            if(root->l==NULL && root->r==NULL)    return 1; // 是叶子节点 
            return getLeafNum(root->l) + getLeafNum(root->r); // 非叶子节点 
        }
        
        int getHeight(Node* root) {
            if(root==NULL)    return 0;
            return max(getHeight(root->l), getHeight(root->r))+1; // 每个节点都加1,所以统计了所有节点数 
        }
        
        void turn_all_node(Node *root) {
            if(root==NULL)    return;
            if(root->l==NULL && root->r==NULL)    return; // 两个分支均为空,交换无意义 
            // 左右分支交换 
            Node* temp = root->l;
            root->l = root->r;
            root->r = temp;
            if(root->l)    turn_all_node(root->l);
            if(root->r)    turn_all_node(root->r);
        }
        
        bool isExist(Node *root, int x) {
            if(root == NULL)    return 0;
            if(root->x == x)    return 1;
            return isExist(root->l, x) || isExist(root->r, x);
        }
        // 求二叉树第K层的节点个数
        int getKnum(Node *root, int k) {
            if(root==NULL || k < 1)    return 0;
            if(k==1)    return 1;
            return getKnum(root->l, k-1)+getKnum(root->r, k-1);
        }
        // 判断两棵二叉树是否结构相同
        bool cmp(Node *a, Node *b) {
            if(a == NULL && b == NULL) return 1; 
            if(a==NULL || b==NULL)    return 0;
            return cmp(a->l, b->l) && cmp(a->r, b->r);
        }
    private:
        Node *root;
};

int main ()
{
    // 1 2 4 -1 -1 5 -1 6 -1 -1 3 7 -1 8 -1 -1 -1
    // 6 4 2 3 -1 -1 -1 -1 5 1 -1 -1 7 -1 -1
    Node* root;
    Tree tree(root);
    root = tree.buildTree();
    
    // 前、中、后序遍历 
    tree.print_pre_order(root);
    printf("\n");
    
    tree.print_in_order(root);
    printf("\n");
    
    tree.print_post_order(root);
    printf("\n");
    
    tree.print_level_order(root);
    printf("\n");
    
    //节点个数
    cout << "Node num: " << tree.getNodeNum(root) << endl;
     
    // 叶子个数 
    cout << "Leaf num: " << tree.getLeafNum(root) << endl;
    
    // 树高 
    cout << "Height: " << tree.getHeight(root) << endl;
    
    // 二叉树镜像 
    tree.turn_all_node(root);
    tree.print_level_order(root);
    printf("\n");
    
    //子树的节点查找 
//    int x;
//    while(cin >> x) {
//        printf("%s\n", tree.isExist(root, x) ? "Yes!":"No.");
//    }

    //求二叉树第K层的节点个数
//    int k;
//    while(cin >> k) {
//        printf("The %dth floor nodes num: %d\n", k, tree.getKnum(root, k));
//    }

    return 0;
}

 

转载于:https://www.cnblogs.com/AcIsFun/p/5303511.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(1)非递归定义 树(tree)是由n(n≥0)个结点组成的有限集合。n=0的树称为空树;n>0的树T: ① 有且仅有一个结点n0,它没有前驱结点,只有后继结点。n0称作树的根(root)结点。 ② 除结点外n0 , 其余的每一个结点都有且仅有一个直接前驱结点;有零个或多个直接后继结点。 (2)递归定义 一颗大树分成几个大的分枝,每个大分枝再分成几个小分枝,小分枝再分成更小的分枝,… ,每个分枝也都是一颗树,由此我们可以给出树的递归定义。 树(tree)是由n(n≥0)个结点组成的有限集合。n=0的树称为空树;n>0的树T: ① 有且仅有一个结点n0,它没有前驱结点,只有后继结点。n0称作树的根(root)结点。 ② 除根结点之外的其他结点分为m(m≥0)个互不相交的集合T0,T1,…,Tm-1,其中每个集合Ti(0≤i<m)本身又是一棵树,称为根的子树(subtree)。 2、掌握树的各种术语: (1) 父母、孩子与兄弟结点 (2) 度 (3) 结点层次、树的高度 (4) 边、路径 (5) 无序树、有序树 (6) 森林 3、二叉树的定义 二叉树(binary tree)是由n(n≥0)个结点组成的有限集合,此集合或者为空,或者由一个根结点加上两棵分别称为左、右子树的,互不相交的二叉树组成。 二叉树可以为空集,因此根可以有空的左子树或者右子树,亦或者左、右子树皆为空。 4、掌握二叉树的五个性质 5、二叉树的二叉链表存储。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值