1. 程式人生 > >牛客網二叉樹程式設計題

牛客網二叉樹程式設計題

樹的高度

#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;
}