Niuke.com (binary tree)

https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef?tpId=60&&tqId=29483&rp=1&ru=/activity/oj&qru=/ta/tsing-kaoyan/question-ranking 

Compared with leetcode, this question is a little different. We need to write the interface function ourselves, so there are some troubles. We have to write a function for in-order traversal to do the final output, and we also have to write a function to store the characters. You have to write an interface function to create nodes. This topic is very similar to how to create nodes in a binary tree. Let's analyze the interfaces one by one. First, I will directly give you the structure you need.

typedef struct TreeNode
{
    char val;
    struct TreeNode* left;
    struct TreeNode* right;
}TreeNode;
 

This is the content of a very basic structure, because every time we put a character into it, we need to create a node. In this way, we can continue to write a function that creates nodes. Let's take a look. ,

TreeNode* CreatNode(char x)
{
    TreeNode* newnode = (TreeNode*)malloc(sizeof(TreeNode));
    newnode->val = x;
    newnode->left = newnode->right = NULL;
    return newnode;
}

 

The next step is actually how we put these characters into this. We can easily understand it when talking about the code.

TreeNode* maketree(char* a, int* pi)
{
    if(a[*pi] == '#')
    {
        (*pi)++;
        return NULL;
    }
    TreeNode* root = CreatNode(a[(*pi)++]);
    root->left = maketree(a, pi);
    root->right = maketree(a, pi);
    return root;
}

 

 Our problem is how to link a parent node and two child nodes. When we encounter the character '#', we have to return a null pointer. If it is not null, we have to create a node, put the value in, and then proceed It's OK to traverse, but we need to link, so the left and right of the root should point to the place where our function is recursive. You can also draw a recursive expansion diagram to understand it.

 

Our preorder storage needs to become a tree like this.

Complete code

#include<stdio.h>
typedef struct TreeNode
{
    char val;
    struct TreeNode* left;
    struct TreeNode* right;
}TreeNode;
 
TreeNode* CreatNode(char x)
{
    TreeNode* newnode = (TreeNode*)malloc(sizeof(TreeNode));
    newnode->val = x;
    newnode->left = newnode->right = NULL;
    return newnode;
}
TreeNode* maketree(char* a, int* pi)
{
    if(a[*pi] == '#')
    {
        (*pi)++;
        return NULL;
    }
    TreeNode* root = CreatNode(a[(*pi)++]);
    root->left = maketree(a, pi);
    root->right = maketree(a, pi);
    return root;
}
void Inorder(TreeNode* root)
{
    if(root == NULL)
    {
        return ;
    }
    Inorder(root->left);
    printf("%c ",root->val);
    Inorder(root->right);
}
int main()
{
    char arr[101];
    scanf("%s",arr);
    int count = 0;
    TreeNode* tree = maketree(arr,&count);
    Inorder(tree);
    return 0;
}

 

Another day of water articles. 

Guess you like

Origin blog.csdn.net/2301_76895050/article/details/134922899