【数据结构】二叉树相关题目(2)

本文详细介绍了二叉树的前序、中序和后序遍历算法,以及如何检测一颗树是否是另一棵树的子树,以及如何判断平衡二叉树。代码示例使用C++实现,展示了遍历过程和相关数据结构的使用。
摘要由CSDN通过智能技术生成

一、二叉树的前序遍历

 思路:先求出树的结点个数,开好数组之后进行前序遍历即可。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */

int TreeSide(struct TreeNode* root)
{
    if(root == NULL)
    {
        return 0;
    }

    return TreeSide(root->left) + TreeSide(root->right) + 1;
}

void _preorderTraversal(struct TreeNode* root, int*arr, int*pi)
{
    if(root == NULL)
    {
        return;
    }

    arr[(*pi)++] = root->val;
    _preorderTraversal(root->left,arr,pi);
    _preorderTraversal(root->right,arr,pi);

}

int* preorderTraversal(struct TreeNode* root, int* returnSize) 
{
    *returnSize = TreeSide(root);
    int* arr = (int*)malloc(*returnSize * sizeof(int));
    int i = 0;
    _preorderTraversal(root,arr,&i);
    return arr;
}

二、二叉树的中序遍历

  思路:先求出树的结点个数,开好数组之后进行中序遍历即可。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int Treeside(struct TreeNode* root)
{
    if(root == NULL)
    {
        return 0;
    }

    return Treeside(root->left) + Treeside(root->right) + 1;
}

void _inorderTraversal(struct TreeNode* root, int*arr, int* pi)
{
    if(root == NULL)
    {
        return;
    }

    _inorderTraversal(root->left, arr, pi);
    arr[(*pi)++] = root->val;
    _inorderTraversal(root->right, arr, pi);

}

int* inorderTraversal(struct TreeNode* root, int* returnSize) 
{
    *returnSize = Treeside(root);
    int* arr = (int*)malloc(*returnSize * sizeof(int));
    int i = 0;
    _inorderTraversal(root,arr,&i);
    return arr;
}

三、二叉树的后序遍历 

   思路:先求出树的结点个数,开好数组之后进行后序遍历即可。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */

int Treeside(struct TreeNode* root)
{
    if(root == NULL)
    {
        return 0;
    }

    return Treeside(root->left) + Treeside(root->right) + 1;
}

void _postorderTraversal(struct TreeNode* root, int*arr, int* pi)
{
    if(root == NULL)
    {
        return;
    }

    _postorderTraversal(root->left, arr, pi);
    _postorderTraversal(root->right, arr, pi);
    arr[(*pi)++] = root->val;

}

int* postorderTraversal(struct TreeNode* root, int* returnSize) 
{
    *returnSize = Treeside(root);
    int* arr = (int*)malloc(*returnSize * sizeof(int));
    int i = 0;
    _postorderTraversal(root,arr,&i);
    return arr;
}

四、另一颗树的子树 

思路:每一层函数栈帧中都包括,如果 root 等于空,返回 false,如果调用相同的树为真,返回 true。每一层往下判断是否为相同的树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

bool isSameTree(struct TreeNode* p, struct TreeNode* q) 
{
    if(p == NULL && q == NULL)
    {
        return true;
    }     

    if(p == NULL || q == NULL)
    {
        return false;
    }

    if(p->val != q->val)
    {
        return false;
    }
    return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}


bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot)
{
    if(root == NULL)
    {
        return false;
    }

    if(isSameTree(root, subRoot))
    {
        return true;
    }

    return isSubtree(root->left,subRoot) || isSubtree(root->right, subRoot);
}

五、二叉树遍历

 先前序构建树,再中序输出树即可

#include <stdio.h>
#include <stdlib.h>
typedef char BTDataType;
typedef struct BinaryTreeNode
{
    BTDataType data;
    struct BinaryTreeNode* left;
    struct BinaryTreeNode* right;
}BTNode;

BTNode* BuyNode(BTDataType x)
{
    BTNode* node = (BTNode*)malloc(sizeof(BTNode));
    if(node == NULL)
    {
        perror("BuyNode");
        return NULL;
    }
    node->left = NULL;
    node->right = NULL;
    node->data = x;
    return node;
}

BTNode* CreateBTree(char a[], int* pi)
{
    if(a[*pi] == '#')
    {
        (*pi)++;
        return NULL;
    }

    BTNode* root = BuyNode(a[*pi]);
    (*pi)++;
    root->left = CreateBTree(a,pi);
    root->right = CreateBTree(a,pi);
    return root;
}

void InOrder(BTNode* root)
{
    if(root == NULL)
    {
        return;
    }

    InOrder(root->left);
    printf("%c ",root->data);
    InOrder(root->right);
}

int main() 
{
    char a[100] = {0};
    scanf("%s",a);
    int i = 0;
    BTNode* root = CreateBTree(a,&i);
    InOrder(root);
    printf("\n");
    return 0;
}

六、平衡二叉树

 思路:用求树深度的方式判断每个节点的子树的高度差是否小于等于1即可

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

int TreeHeap(struct TreeNode* root)
{
    if(root == NULL)
    {
        return 0;
    }

    int leftnode = TreeHeap(root->left);
    int rightnode = TreeHeap(root->right);

    return leftnode>rightnode ? leftnode+1 : rightnode+1;
}

bool isBalanced(struct TreeNode* root) 
{
    if(root == NULL)
    {
        return true;
    }
    int lefttree = TreeHeap(root->left);
    int righttree = TreeHeap(root->right);
   
   if(abs(lefttree - righttree) > 1)
   {
        return false; 
   }

    return isBalanced(root->left) && isBalanced(root->right);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值