【数据结构】二叉树基础OJ

简介: 【数据结构】二叉树基础OJ

1. 单值二叉树

链接:力扣

代码1:

1. /**
2.  * Definition for a binary tree node.
3.  * struct TreeNode {
4.  *     int val;
5.  *     struct TreeNode *left;
6.  *     struct TreeNode *right;
7.  * };
8.  */
9. bool isUnivalTree(struct TreeNode* root)
10. {
11. if (root == NULL)
12.     {
13. return true;
14.     }
15. if (root->left && root->val != root->left->val)
16.     {
17. return false;
18.     }
19. if (root->right && root->val != root->right->val)
20.     {
21. return false;
22.     }
23. return isUnivalTree(root->left) && isUnivalTree(root->right);
24. 
25. }

思想:(1)【左孩子的返回值是否相等+右孩子的返回值是否相等】当左孩子和根比较不相等或者右孩子和根比较不相等,这个树就是不相等的【返回true的条件比较多,所以,返回不相等的条件少】【代码1】(2)遍历,和根的值进行比较

注意:(1)在OJ题中尽量不要定义静态变量和全局变量

2. 检查两颗树是否相同

链接:力扣

代码:

1. /**
2.  * Definition for a binary tree node.
3.  * struct TreeNode {
4.  *     int val;
5.  *     struct TreeNode *left;
6.  *     struct TreeNode *right;
7.  * };
8.  */
9. bool isSameTree(struct TreeNode* p, struct TreeNode* q)
10. {
11. if (p == NULL && q == NULL)
12.     {
13. return true;
14.     }
15. if (p == NULL || q == NULL)//一个为空,一个不为空
16.     {
17. return false;
18.     }
19. if (p->val != q->val)
20.     {
21. return false;
22.     }
23. return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
24. }

思想:递归;分治

3. 对称二叉树

链接:力扣

代码:

1. /**
2.  * Definition for a binary tree node.
3.  * struct TreeNode {
4.  *     int val;
5.  *     struct TreeNode *left;
6.  *     struct TreeNode *right;
7.  * };
8.  */
9. bool _isSymmetric(struct TreeNode* p, struct TreeNode* q)
10. {
11. //
12. if (p == NULL && q == NULL)
13.     {
14. return true;
15.     }
16. //其中一个为空
17. if (p == NULL || q == NULL)
18.     {
19. return false;
20.     }
21. return p->val == q->val && _isSymmetric(p->left, q->right) && _isSymmetric(p->right, q->left);
22. }
23. 
24. bool isSymmetric(struct TreeNode* root)
25. {
26. if (root == NULL)
27.     {
28. return true;
29.     }
30. return _isSymmetric(root->left, root->right);
31. 
32. }

思想:首先,判断树的左子树和右子树是否相等;然后,判断左子树的左子树和右子树的右子树以及左子树的右子树和右子树的左子树是否相等,以及重复这个操作

4. 二叉树的前序遍历

链接:力扣

代码:

1. /**
2.  * Definition for a binary tree node.
3.  * struct TreeNode {
4.  *     int val;
5.  *     struct TreeNode *left;
6.  *     struct TreeNode *right;
7.  * };
8.  */
9. /**
10.  * Note: The returned array must be malloced, assume caller calls free().
11.  //意思是返回值可以被malloc,假设由调用者释放
12.  */
13. //首先确定二叉树的节点个数
14. int BinaryTreeSize(struct TreeNode* root)
15. {
16. return root == NULL ? 0 : (BinaryTreeSize(root->left)+BinaryTreeSize(root->right) + 1);
17. }
18. 
19. int* preorder(struct TreeNode* root, int* a, int* pi)
20. {
21. if (root == NULL)
22.     {
23. return;
24.     }
25.     a[*pi] = root->val;
26.     (*pi)++;
27. preorder(root->left, a, pi);
28. preorder(root->right, a, pi);
29. }
30. 
31. int* preorderTraversal(struct TreeNode* root, int* returnSize)
32. {
33. //定义的是数组的指针,因为定义的如果是数组,出了这个函数,就被销毁了
34. int size = BinaryTreeSize(root);//j节点个数
35. int* a = (int*)malloc(sizeof(int) * size);//定义数组
36. if (a == NULL)
37.     {
38. printf("malloc fail\n");
39. exit(-1);
40.     }
41.     *returnSize = size;
42. //前序遍历,并把结果,给数组,
43. int i = 0;
44. preorder(root,a, &i);
45. return a;
46. }

思想:首先,我们可以非常容易地想到前序遍历的,把之前学到过的前序遍历的printf,改成数组的赋值。看题意,我们需要定义一个数组的指针和二叉树的节点个数,然后尽可以调用前序遍历的函数,进行解决。

注意:returnSize 这个代表的是数组的大小,这个函数,相当于输出型参数,returnSize,外面定义了,但是没有值,需要我们对齐进行赋值。【这里应该用指针,因为形参的改变并不影响实参】

5. 二叉树的中序遍历

链接:力扣

代码:

1. /**
2.  * Definition for a binary tree node.
3.  * struct TreeNode {
4.  *     int val;
5.  *     struct TreeNode *left;
6.  *     struct TreeNode *right;
7.  * };
8.  */
9. 
10. 
11. /**
12.  * Note: The returned array must be malloced, assume caller calls free().
13.  */
14. int BinaryTreeSize(struct TreeNode* root)
15. {
16. return root == NULL ? 0 : (BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1);
17. }
18. 
19. 
20. void inorder(struct TreeNode* root, int* a, int* pi)
21. {
22. if (root == NULL)
23.     {
24. return;
25.     }
26. inorder(root->left, a, pi);
27.     a[*pi] = root->val;
28.     (*pi)++;
29. inorder(root->right, a, pi);
30. }
31. 
32. int* inorderTraversal(struct TreeNode* root, int* returnSize)
33. {
34. int size = BinaryTreeSize(root);
35.     *returnSize = size;
36. int* a = (int*)malloc(sizeof(int) * size);
37. if (a == NULL)
38.     {
39. printf("malloc fail\n");
40. exit(-1);
41.     }
42. int i = 0;
43. inorder(root, a, &i);
44. return a;
45. }

6. 二叉树的后序遍历

链接:力扣

代码:

1. /**
2.  * Definition for a binary tree node.
3.  * struct TreeNode {
4.  *     int val;
5.  *     struct TreeNode *left;
6.  *     struct TreeNode *right;
7.  * };
8.  */
9. 
10. 
11. /**
12.  * Note: The returned array must be malloced, assume caller calls free().
13.  */
14. 
15. int BinaryTreeSize(struct TreeNode* root)
16. {
17. return root == NULL ? 0 : (BinaryTreeSize(root->left) + BinaryTreeSize(root->right) + 1);
18. }
19. 
20. void postorder(struct TreeNode* root, int* a, int* pi)
21. {
22. if (root == NULL)
23.     {
24. return;
25.     }
26. postorder(root->left, a, pi);
27. postorder(root->right, a, pi);
28.     a[*pi] = root->val;
29.     (*pi)++;
30. }
31. 
32. int* postorderTraversal(struct TreeNode* root, int* returnSize)
33. {
34. int size = BinaryTreeSize(root);
35.    *returnSize = size;
36. int* a = (int*)malloc(sizeof(int) * size);
37. int i = 0;
38. postorder(root, a, &i);
39. return a;
40. }

7. 另一颗树的子树

链接:力扣

代码:

1. /**
2.  * Definition for a binary tree node.
3.  * struct TreeNode {
4.  *     int val;
5.  *     struct TreeNode *left;
6.  *     struct TreeNode *right;
7.  * };
8.  */
9. 
10. bool isSameTree(struct TreeNode* p,struct TreeNode* q)
11. {
12. if (p == NULL && q == NULL)
13.     {
14. return true;
15.     }
16. if (p == NULL || q == NULL)
17.     {
18. return false;
19.     }
20. if (p->val != q->val)
21.     {
22. return false;
23.     }
24. return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
25. }
26. 
27. 
28. bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot)
29. {
30. if (root == NULL)
31.     {
32. return false;
33.     }
34. return isSameTree(root, subRoot) || isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
35. }

思想:分治,根中是否有和subRoot相等+左子树中是否有和subRoot相等的部分+左子树中是否有和subRoot相等的部分。

注意:主函数中,我们可能会漏掉if这个条件语句,如果不写这个代码,root->left,就会出现问题。

8. 二叉树的结构及遍历

链接:牛客

代码:

1. #include <stdio.h>
2. #include <stdlib.h>
3. typedef struct BinaryTreeNode
4. {
5. char data;
6. struct BinaryTreeNode* left;
7. struct BinaryTreeNode* right;
8. }BTNode;
9. //先构建一个二叉树【前序遍历】
10. BTNode* CreatTree(char* a, int* pi)
11.  {
12. if (a[*pi] == '#')
13.     {
14.         (*pi)++;
15. return NULL;
16.     }
17. //先构建根
18.     BTNode* root = (BTNode*)malloc(sizeof(BTNode));
19.     root->data = a[*pi];
20.     (*pi)++;
21. //再构建左子树和右子树
22.     root->left = CreatTree(a, pi);
23.     root->right = CreatTree(a, pi);
24. return root;
25.  }
26. 
27. void InOrder(BTNode* root)
28. {
29. if (root == NULL)
30.     {
31. return;
32.     }
33. InOrder(root->left);
34. printf("%c ", root->data);
35. InOrder(root->right);
36. }
37. 
38. int main()
39. {
40. char a[100];
41. scanf("%s", a);
42. int i = 0;
43.     BTNode* tree = CreatTree(a, &i);
44. InOrder(tree);
45. free(tree);
46.     tree = NULL;
47. return 0;
48. }

思想:先序遍历的思想的字符串,建立二叉树【遇到'#',就返回NULL】,然后再中序遍历的思想进行打印。

相关文章
|
8天前
【数据结构】二叉树(遍历,递归)
【数据结构】二叉树(遍历,递归
16 2
|
1天前
|
算法 编译器 C语言
数据结构——二叉树四种遍历的实现-3
数据结构——二叉树四种遍历的实现
数据结构——二叉树四种遍历的实现-3
|
1天前
|
存储
数据结构——二叉树四种遍历的实现-2
数据结构——二叉树四种遍历的实现
数据结构——二叉树四种遍历的实现-2
|
1天前
|
机器学习/深度学习
数据结构——二叉树四种遍历的实现-1
数据结构——二叉树四种遍历的实现
数据结构——二叉树四种遍历的实现-1
|
5天前
【数据结构】二叉树的三种遍历(非递归讲解)
【数据结构】二叉树的三种遍历(非递归讲解)
8 1
|
5天前
|
存储
【数据结构】二叉树相关oj题(一)
【数据结构】二叉树相关oj题(一)
9 1
|
6天前
|
容器
【栈与队列】栈与队列的相互转换OJ题
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
10 0
|
8天前
|
存储 分布式数据库
[数据结构]~二叉树
[数据结构]~二叉树
|
8天前
|
C语言
猿创征文|栈和队列OJ刷题
猿创征文|栈和队列OJ刷题
|
8天前
|
C语言
【C语言/数据结构】二叉树(层序遍历|判断完全二叉树|性质)
【C语言/数据结构】二叉树(层序遍历|判断完全二叉树|性质)
280 52