二叉树基础oj练习

1.单值二叉树

题目:

力扣https://leetcode.cn/problems/univalued-binary-tree/

思路:

单值二叉树 = root和左右孩子的值相等 + 左子树是单值二叉树 + 右子树是单值二叉树

代码:

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


bool isUnivalTree(struct TreeNode* root)
{
    if(root==NULL)
        return true;
    if(root->left&&root->left->val!=root->val)
        return false;
     if(root->right&&root->right->val!=root->val)
        return false;
    return isUnivalTree(root->left)&&isUnivalTree(root->right);
}

2.相同的树

题目:

力扣https://leetcode.cn/problems/same-tree/submissions/

思路:

相同二叉树 = 比较两棵树的根 + 递归左子树(是否是相同二叉树)+ 递归右子树(是否是相同二叉树)

代码:

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

3.对称二叉树

 题目:

力扣https://leetcode.cn/problems/symmetric-tree/

思路:

解题方法和上一道题相似

原来的函数接口上是递归不起来的,因此要写一个子函数。

代码:

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

 bool _isSymmetricTree(struct TreeNode* root1,struct TreeNode*root2)
 {
  if(root1==NULL&&root2==NULL)
        return true;
    if(root1==NULL||root2==NULL)
        return false;
    if(root1->val!=root2->val)
        return false;
    return _isSymmetricTree(root1->left,root2->right)
         &&_isSymmetricTree(root1->right,root2->left);
 }

bool isSymmetric(struct TreeNode* root)
{
  if(root==NULL)
  return true;

  return _isSymmetricTree(root->left,root->right);
}

4.二叉树的前序遍历

题目:

力扣https://leetcode.cn/problems/binary-tree-preorder-traversal/

思路:

  • 计算二叉树的节点个数
  • 创建新的数组,数组大小为二叉树总结点大小
  • 前序遍历根,左,右

 代码:

/**
 * 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 TreeSize(struct TreeNode*root)
 {
     return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
 }

//创建新的数组,数组大小为二叉树总结点大小
void _preorderTraversal(struct TreeNode*root,int*a,int*pi)
{
    if(root==NULL)
    return;
    //进行前序遍历根,左,右
    printf("%d ",root->val);
    a[(*pi)++]=root->val;
    _preorderTraversal(root->left,a,pi);
    _preorderTraversal(root->right,a,pi);
}

int* preorderTraversal(struct TreeNode* root, int* returnSize)
{
    int size=TreeSize(root);
    printf("size:%d\n",size);
    int*a=malloc(sizeof(int)*size);
    int i=0;
    _preorderTraversal(root,a,&i);
    *returnSize=size;
    return a;
}

5. 二叉树中序遍历 

题目:

力扣https://leetcode.cn/problems/binary-tree-inorder-traversal/

思路:

  • 计算二叉树的节点个数
  • 创建新的数组,数组大小为二叉树总结点大小
  • 中序遍历左,根,右

代码:

/**
 * 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 TreeSize(struct TreeNode*root)
 {
     return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
 }

//创建新的数组,数组大小为二叉树总结点大小
void _inorderTraversal(struct TreeNode*root,int*a,int*pi)
{
    if(root==NULL)
    return;
    //进行中序遍历左,根,右
    printf("%d ",root->val);
    _inorderTraversal(root->left,a,pi);
     a[(*pi)++]=root->val;
    _inorderTraversal(root->right,a,pi);
}

int* inorderTraversal(struct TreeNode* root, int* returnSize)
{
     int size=TreeSize(root);
    printf("size:%d\n",size);
    int*a=malloc(sizeof(int)*size);
    int i=0;
    _inorderTraversal(root,a,&i);
    *returnSize=size;
    return a;
}

 6.二叉树后序遍历 

题目:

力扣https://leetcode-cn.com/problems/binary-tree-postorder-traversal/

思路:

  • 计算二叉树的节点个数
  • 创建新的数组,数组大小为二叉树总结点大小
  • 后序遍历左,右,根,

代码:

/**
 * 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 TreeSize(struct TreeNode*root)
 {
     return root==NULL?0:TreeSize(root->left)+TreeSize(root->right)+1;
 }

//创建新的数组,数组大小为二叉树总结点大小
void _postorderTraversal(struct TreeNode*root,int*a,int*pi)
{
    if(root==NULL)
    return;
    //进行中序遍历左,根,右
    printf("%d ",root->val);
    _postorderTraversal(root->left,a,pi);
    _postorderTraversal(root->right,a,pi);
     a[(*pi)++]=root->val;
}

int* postorderTraversal(struct TreeNode* root, int* returnSize)
{
    int size=TreeSize(root);
    printf("size:%d\n",size);
    int*a=malloc(sizeof(int)*size);
    int i=0;
    _postorderTraversal(root,a,&i);
    *returnSize=size;
    return a;
}

7.另一颗树的子树

题目:

力扣https://leetcode-cn.com/problems/subtree-of-another-tree/

思路:

 思路: 相同的树+递归左子树+递归右子树

代码: 

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

8.二叉树遍历

题目:

二叉树遍历_牛客题霸_牛客网【牛客题霸】收集各企业高频校招笔面试题目,配有官方题解,在线进行百度阿里腾讯网易等互联网名企笔试面试模拟考试练习,和牛人一起讨论经典试题,全面提升你的技术能力https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef?tpId=60&&tqId=29483&rp=1&ru=/activity/oj&qru=/ta/tsing-kaoyan/question-ranking

思路:

1.建立字符型数组

2.构建一个二叉树,把数组的值输按前序遍历输入到二叉树中,当遇到#时,代表该节点为空

3.将前序遍历好的二叉树,进行中序遍历

代码:

#include
#include
#include

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

TreeNode* CreateTree(char* str,int* pi)
{
    if(str[*pi] == '#')
    {
        (*pi)++;
        return NULL;
    }
    TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
    root->val = str[(*pi)++];
    root->left = CreateTree(str, pi);
    root->right = CreateTree(str, pi);
    return root;
}


void InOrder(TreeNode* root)
{
	if (root == NULL)
	{
		return;
	}
	InOrder(root->left);
	printf("%c ", root->val);
	InOrder(root->right);
}

int main()
{
    char str[100];
    while(scanf("%s",str)!=EOF)
    {
        int i = 0;
        TreeNode* root = CreateTree(str, &i);
        InOrder(root);
    }
    return 0; 
}

你可能感兴趣的:(数据结构刷题,数据结构)