C++面试准备之二叉树操作

C++面试准备之二叉树操作

二叉树节点定义

struct BNode{
    int data;
    BNode* left;
    BNode* right;
};

创建二叉树

BNode* createNode() {
    BNode* t;
    int c;
    cin >> c;
    if(c == 0) {
        t = NULL;
    }else{
        t = (BNode*)malloc(sizeof(BNode));
        t->data = c;
        t->left = createNode();
        t->right = createNode();
    }
    return t;
}

前序遍历(递归)

void preorder(BNode* root) {
    if(root) {
        cout << root->data << " ";
        preorder(root->left);
        preorder(root->right);
    }
}

中序遍历(递归)

void inorder(BNode* root) {
    if(root) {
        inorder(root->left);
        cout << root->data << " ";
        inorder(root->right);
    }
}

后序遍历(递归)

void postorder(BNode* root) {
    if(root) {
        postorder(root->left);
        postorder(root->right);
        cout << root->data << " ";
    }
}

前序遍历(非递归)

void preorder2(BNode* root) {
    stack<BNode*> s;
    s.push(root);
    while(!s.empty()){
        BNode* cur = s.top();
        s.pop();
        if(cur) {
            cout << cur->data << " ";
            s.push(cur->right);
            s.push(cur->left);
        }
    }
}

中序遍历(非递归)

void inorder(BNode* root) {
    stack<BNode*> s;
    BNode* p = root;
    while(!s.empty() || p != NULL) {
        if(p == NULL) {
            BNode* cur = s.top();
            s.pop();
            cout << cur->data << " ";
            p = cur->right;
        }else{
            s.push(p);
            p = p->left;
        }
    }
}

后续遍历(非递归,用两个栈)

void postorder2(BNode* root) {
    stack<BNode*> sta1;
    stack<BNode*> sta2;
    sta1.push(root);
    while(!sta1.empty()) {
        BNode* cur = sta1.top();
        sta1.pop();
        sta2.push(cur);
        if(cur->left) sta1.push(cur->left);
        if(cur->right) sta1.push(cur->right);
    }
    while(!sta2.empty()) {
        BNode* tmp = sta2.top();
        sta2.pop();
        cout << tmp->data << " ";
    }
}

后序遍历(非递归,一个栈实现)

void postorder2_1(BNode* root) {
    stack<BNode*> s;
    BNode* c = root;
    s.push(root);
    while(!s.empty()) {
        BNode* cur = s.top();
        if( (cur->left == NULL && cur->right == NULL) || 
            (c != NULL && (c == cur->left || c == cur->right)) )
        {
            cout << cur->data << " ";
            s.pop();
            c = cur;
        }
        else{
            if( cur->right != NULL )
                s.push(cur->right);
            if( cur->left != NULL)
                s.push(cur->left);
        }
    }
}

二叉树节点总数

int NodeNum(BNode* root) {
    if(root == NULL) {
        return 0;
    }else{
        return 1 + NodeNum(root->left) + NodeNum(root->right);
    }
}

二叉树深度

int depthTree(BNode* root) {
    if(root == NULL) {
        return 0;
    }else{
        return depthTree(root->left) > depthTree(root->right)?1+depthTree(root->left) : 1+depthTree(root->right);
    }
}

二叉树的叶子数

int leafNum(BNode* root) {
    if(root == NULL) {
        return 0;
    }
    else if(root->left == NULL && root->right == NULL) {
        return 1;
    }
    else {
        return (leafNum(root->left) + leafNum(root->right));
    }
}

二叉树的宽度

int treeWidth(BNode* root) {
    if(root == NULL) return 0;
    int nLastLevelWidth = 0;
    int nCurLevelWidth = 0;
    queue<BNode*> q;
    q.push(root);
    int nWidth = 1;
    nLastLevelWidth = 1;
    BNode* cur = NULL;

    while(!q.empty()) {
        while(nLastLevelWidth != 0) {
            cur = q.front();
            q.pop();
            if(cur->left != NULL)
                q.push(cur->left);
            if(cur->right != NULL)
                q.push(cur->right);
            nLastLevelWidth--;
        }
        nCurLevelWidth = q.size();
        nWidth = nCurLevelWidth > nWidth ? nCurLevelWidth : nWidth;
        nLastLevelWidth = nCurLevelWidth;
    }
    return nWidth;
}   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值