牛客网二叉树编程题

树的高度

#include <iostream>
#include <vector>
using namespace std;
int main() {
    int n, H = 1;
    int f, c, h;
    vector<int> nodes(1000, 0);   // 有效节点的高度
    nodes[0] = 1;       // 题目说至少有一个节点,高度是1
    vector<int> childnum(1000, 0); // 记录节点孩子的数量
    cin >> n;
    while (n--) {
        cin >> f >> c;
        // 父节点不存在 或者父节点已经有两个孩子 就跳过
        if (nodes[f] == 0 || childnum[f] == 2)
            continue;
        childnum[f] += 1;
        h = nodes[f] + 1;
        nodes[c] = h;
        if (h > H)
            H = h;
    }
    cout << H << endl;
    return 0;
}

二叉排序树

#include <iostream>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void insert(int v,TreeNode *root){
    if(v == root->val)
        return;   //题目里说:输出的二叉树遍历序列中重复元素不用输出
    if(v < root->val){//小于根节点,往左
        if(!root->left)
            root->left = new TreeNode(v);  //左子树为空,以v值构造一个节点挂上去
        else
            insert(v,root->left);  //左子树不为空,继续插入
    }else{   //大于根节点,往右
        if(!root->right)
            root->right = new TreeNode(v);  //右子树为空,以v值构造一个节点挂上去
        else
            insert(v,root->right);  //右子树不为空,继续插入
    }
}
void preorderTraversal(TreeNode *root){    // 前序遍历
    if(!root) return;
    cout << root->val << " ";
    preorderTraversal(root->left);
    preorderTraversal(root->right);
}
void inorderTraversal(TreeNode *root){     // 中序遍历
    if(!root) return;
    inorderTraversal(root->left);
    cout << root->val << " ";
    inorderTraversal(root->right);
}
void postorderTraversal(TreeNode *root){    //后序遍历
    if(!root) return;
    postorderTraversal(root->left);
    postorderTraversal(root->right);
    cout << root->val << " ";
}
int main(){
    int n;
    while(cin >> n){
        n--;
        int val;
        cin >> val;
        TreeNode root(val);
        while(n--){
            cin >> val;
            insert(val,&root);
        }
        preorderTraversal(&root);cout << endl;
        inorderTraversal(&root);cout << endl;
        postorderTraversal(&root);cout << endl;
    }
    return 0;
}

二叉树遍历

#include<stdio.h>
#include<string.h>
char pre[26];
char in[26];
char post[26];
//  三个low和high分别是pre,in和post的起止位置
void Post(int low1, int high1, int low2, int high2, int low, int high) {
    if(low > high)
        return;
    char c = pre[low1];
    post[high] = c;   // 把pre[]第一个字符赋给post[]最后一个位置
    int k = 0;
    while(in[low2+k] != c)   // 找到pre[]第一个字符在in[]中的位置
        k++;
    Post(low1+1, low1+k, low2, low2+k-1, low, low+k-1);
    Post(low1+k+1, high1, low2+k+1, high2, low+k, high-1);
    return;
}

int main() {
    while(scanf("%s", pre) != EOF) {
        scanf(" %s", in);
        int len = strlen(pre);
        Post(0, len-1, 0, len-1, 0, len-1);
        printf("%s", post);
        printf("\n");
    }
    return 0;
}

二叉排序树

#include <iostream>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
void insert(int v,TreeNode *root){
    if(v == root->val)
        return;   //题目里说:输出的二叉树遍历序列中重复元素不用输出
    if(v < root->val){//小于根节点,往左
        if(!root->left) {
            root->left = new TreeNode(v);  //左子树为空,以v值构造一个节点挂上去
            cout << root->val << endl;
        }
        else
            insert(v,root->left);  //左子树不为空,继续插入
    }else{   //大于根节点,往右
        if(!root->right) {
            root->right = new TreeNode(v);  //右子树为空,以v值构造一个节点挂上去
            cout << root->val << endl;
        }

        else
            insert(v,root->right);  //右子树不为空,继续插入
    }
}

int main(){
    int n;
    while(cin >> n){
        n--;
        int val;
        cin >> val;
        TreeNode root(val);
        cout << "-1" << endl;
        while(n--){
            cin >> val;
            insert(val,&root);
        }
    }
    return 0;
}

树查找

#include <iostream>
#include <cmath>
using namespace std;
int main() {
    int a[1005];
    int n, i;
    while (cin >> n) {
        for (i = 0; i < n; i++) {
            cin >> a[i];
        }
        int depth;
        cin >> depth;
        if ((pow(2, depth-1)-1) > n) {
            cout << "EMPTY";
        } else {
            if ((pow(2, depth)-1) > n) {
                for (i = pow(2, depth-1)-1; i < n-1; i++) {
                    cout << a[i] << " ";
                }
                cout << a[i];
            } else {
                for (i = pow(2, depth-1)-1; i < pow(2, depth)-1-1; i++) {
                    cout << a[i] << " ";
                }
                cout << a[i];
            }
        }
        cout << endl;
    }
    return 0;
}

哈夫曼树

#include <iostream>
#include <queue>
using namespace std;
int main() {
    int n;

    while (cin >> n) {
        priority_queue< int, vector<int>, greater<int> > q;
        int x;
        for (int i = 0; i < n; i++) {
            cin >> x;
            q.push(x);
        }
        int weight = 0;
        while (q.size() != 1) {
            int a = q.top();
            q.pop();
            int b = q.top();
            q.pop();
            q.push(a+b);
            weight = weight + a + b;
        }
        cout << weight << endl;
    }
    return 0;
}

二叉搜索树

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct TreeNode {
    TreeNode* left;
    TreeNode* right;
    int val;
    TreeNode(int x):val(x), left(NULL), right(NULL){}
};
void insert(int v, TreeNode* root) {
    if (root->val == v) {
        return;
    }
    if (root->val > v) {
        if (root->left == NULL) {
            root->left = new TreeNode(v);
        } else
            insert(v, root->left);
    } else {
        if (root->right == NULL) {
            root->right = new TreeNode(v);
        } else
            insert(v, root->right);
    }
}
string PreOrder(TreeNode* root) {
    string str = "";
    if (root != NULL) {
        str += root->val + '0';
        str += PreOrder(root->left);
        str += PreOrder(root->right);
    }
    return str;
}
string InOrder(TreeNode* root) {
    string str = "";
    if (root != NULL) {
        str += PreOrder(root->left);
        str += root->val + '0';
        str += PreOrder(root->right);
    }
    return str;
}

int main() {
    int n;
    while (scanf("%d", &n)!=EOF) {
        string a;
        cin >> a;
        int len = a.length();
        TreeNode *roota = new TreeNode(a[0]-'0');
        for (int i = 1; i < len; i++) {
            insert(a[i]-'0', roota);
        }
        while (n--) {
            cin >> a;
            TreeNode *rootb = new TreeNode(a[0]-'0');
            for (int i = 1; i < len; i++) {
                insert(a[i]-'0', rootb);
            }
            if (PreOrder(roota) != PreOrder(rootb) || InOrder(roota) != InOrder(rootb))
                cout << "NO" << endl;
            else
                cout << "YES" << endl;
        }
    }
    return 0;
}

二叉树

#include <iostream>
using namespace std;
int countNodes(int m, int n) {
    if (m > n)
        return 0;
    return countNodes(m*2, n) + countNodes(m*2+1, n) + 1;
}
int main() {
    int m, n;
    while (scanf("%d%d", &m, &n) == 2) {
        cout << countNodes(m, n) << endl;
    }
    return 0;
}

二叉树

#include <iostream>
#include <set>
using namespace std;
int main() {
    int x, y;
    while (scanf("%d%d", &x, &y) == 2) {
        set<int> s;
        while (x != 0) {
            s.insert(x);
            x /= 2;
        }
        while (y != 0) {
            if (s.count(y)) {
                cout << y << endl;
                break;
            }
            y /= 2;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/beashaper_/article/details/80716185