Hackerrank-Data structures-Tree

基本的简单题目,很适合数据结构入门用,基本就是先中后遍历、层序遍历、测树的高度、打印树的左右侧节点、插入、交换子树、哈夫曼解码等等基础操作
难度是Moderate的
1.Tree: Huffman Decoding

/*
名称:Tree: Huffman Decoding
难度:Moderate
类别:/
思路:题目本身不难,弄懂题意就好:按照他的意思将哈夫曼编码解码即可
*/

#include
#include
using namespace std;
struct node
{
    int freq;
    char data;
    node * left;
    node * right;
};
void decode_huff(node* root,string s)
{
    int i = 0;
    node* temp=root;
    string result = "";
    while (i < s.length())
    {
        while (temp->data == '\0')
        {
            if (s[i] == '1')
            {
                temp = temp->right;
                i++;
            }
            else
            {
                temp = temp->left;
                i++;
            }
        }
        result =result+temp->data;
        temp = root;
    }
    cout << result;
}

int main()
{
    char a = '\0';
    cout << a;
    return 0;
}

比较综合的
1.Swap Nodes [Algo]

/*
名称:Swap Nodes [Algo]
难度:Easy
类别:/
思路:题目本身也不难,但把建树、交换。遍历融合一起用(感觉像是期末考试题)
*/
#include
#include 
using namespace std;
struct Node
{
    int data;
    int height;
    Node *left;
    Node *right;
};

Node* Create(Node *root)
{
    int N;
    int left, right;
    int front = 0, rear = 0;
    Node *queue[2000];
    queue[rear++] = root;
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        Node *Leftchild = NULL, *Rightchild = NULL;
        Node *parent = queue[front++];
        cin >> left >> right;
        if (left != -1)
        {
            Leftchild = (Node*)malloc(sizeof(Node));
            Leftchild->data = left;
            Leftchild->left = NULL;
            Leftchild->right = NULL;
            Leftchild->height = parent->height + 1;

            queue[rear++] = Leftchild;
        }
        if (right != -1)
        {
            Rightchild = (Node*)malloc(sizeof(Node));
            Rightchild->data = right;
            Rightchild->left = NULL;
            Rightchild->right = NULL;
            Rightchild->height = parent->height + 1;

            queue[rear++] = Rightchild;
        }
        parent->left = Leftchild;
        parent->right = Rightchild;
    }
    return root;
}
void Mid(Node* root)
{
    if (root->left) Mid(root->left);
    cout << root->data<<' ';
    if (root->right) Mid(root->right);
}
Node* swap(Node *root,int height)
{
    Node *temp = root;
    if (root->height == height)
    {
        Node *temp2 = root->left;
        root->left = root->right;
        root->right = temp2;
        return root;
    }
    else
    {
        if (temp->left) temp->left = swap(temp->left, height);
        if (temp->right) temp->right = swap(temp->right, height);
    }
    return root;
}

int main() 
{
    int T, height;
    Node *root = (Node*)malloc(sizeof(Node));
    root->data = 1;
    root->height = 1;
    root->left = NULL;
    root->right = NULL;
    root = Create(root);
    cin >> T;
    for (int i = 0; i < T; i++)
    {
        cin >> height;
        for (int j = 1; j*height <= 1500; j++)
        {
            root = swap(root, j*height);
        }
        Mid(root);      
        cout << endl;
    }


    return 0;
}

难度是Easy的
1.Preorder

void Preorder(node *root) 
{
    if (root)
    {
        cout << root->data<<' ';
        Preorder(root->left);
        Preorder(root->right);
    }
}

2.Postorder

void Postorder(node *root) 
{
    if (root)
    {
        Postorder(root->left);
        Postorder(root->right);
        cout << root->data << ' ';
    }
}

3.Inorder

void Inorder(node *root) 
{
    if (root)
    {
        Inorder(root->left);
        cout << root->data << ' ';
        Inorder(root->right);
    }
}

4.height

int height(node * root)
{
    if (!root)
        return 0;
    else
    {
        int L = height(root->left) + 1;
        int R = height(root->right) + 1;
        if (L >= R)
            return L;
        else
            return R;
    }
}

5.top_view

void top_viewL(node* root)
{
    if (root)
        top_viewL(root->left);
    else
        return;
    cout << root->data << ' ';
}
void top_viewR(node* root)
{
    if (root)
    {
        cout << root->data << ' ';
        top_viewR(root->right);
    }
    else
        return;
}
void top_view(node * root)
{
    top_viewL(root->left);
    cout << root->data << ' ';
    top_viewR(root->right);
}

6.LevelOrder

void LevelOrder(node * root)
{
    node *queue[100];
    int front = 0, rear = 0;
    cout << root->data << ' ';
    queue[rear++] = root->left;
    queue[rear++] = root->right;
    while (front != rear)
    {
        node *temp = queue[front++];
        cout << temp->data << ' ';
        if(temp->left) 
            queue[rear++] = temp->left;
        if (temp->right) 
            queue[rear++] = temp->right;
    }
}

7.insert

node* insert(node * root, int value)
{
    node *temp = root;
    node* insert = (node*)malloc(sizeof(node));
    insert->data = value;
    insert->left = NULL;
    insert->right = NULL;
    if (!root)
        return insert;
    while (true)
    {
        if (value < temp->data)
            if (!temp->left) { temp->left = insert; break; }
            else temp = temp->left;
        else
            if (!temp->right){ temp->left = insert; break; }
            else temp = temp->right;
    }
    return root;
}

8.Lowest Common Ancestor

node * lca(node * root, int v1, int v2)
{
    int max, min=v1+v2;
    node *temp = root;
    v1 > v2 ? max = v1 : max = v2;
    min -= max;
    if (temp->data<max && temp->data>min)
        return temp;
    else
    {
        if (max < temp->data)
            if (temp->left)     temp = lca(temp->left, max, min);   
        else
            if (temp->right)    temp = lca(temp->right, max, min);
    }
    return temp;
}

你可能感兴趣的:(Hackerrank,数据结构,Oj,数据结构及算法笔记,【HackerRank】)