(eden)对称二叉树
Description:
实在不知道出什么题目了,就还是二叉树吧,你们也别嫌烦。
判断一棵二叉树是否对称。二叉树节点定义如上次的结构相同:
typedef struct node {
int x;
struct node* left;
struct node* right;
} BN;
不用关心输入,二叉树构造和删除过程已经在main函数中实现,需要你们实现函数
int isSymmetric(BN* root);
来判断一棵二叉树是否对称,对称返回1,非对称返回0.
node结构要按照上面的代码在symmetric.h中进行定义。
注意被测试二叉树不一定是满二叉树。如果树不存在(根节点指针为空)返回0。如果除根节点外没有任何的其他节点返回1。
代码
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define MAXD 50 4 typedef struct node { 5 int x; 6 struct node* left; 7 struct node* right; 8 }BN; 9 void buildTree(BN** root) { 10 int temp; 11 BN** que[MAXD]; 12 int head = 0; 13 int tail = 1; 14 que[0] = root; 15 scanf("%d", &temp); 16 while (temp != -1) { 17 *que[head] = (BN*)malloc(sizeof(struct node)); 18 (*que[head])->x = temp; 19 que[tail] = &((*que[head])->left); 20 tail = (tail + 1) % MAXD; 21 que[tail] = &((*que[head])->right); 22 tail = (tail + 1) % MAXD; 23 head = (head + 1) % MAXD; 24 scanf("%d", &temp); 25 } 26 while (head != tail) { 27 *que[head] = NULL; 28 head = (head + 1) % MAXD; 29 } 30 } 31 void freeTree(BN* root) { 32 if (root != NULL) { 33 freeTree(root->left); 34 freeTree(root->right); 35 free(root); 36 } 37 } 38 int a[100] = {0}, b[100] = {0}; 39 int atot = 0, btot = 0; 40 void left(BN* k) { 41 if (k -> left != NULL) { 42 left(k -> left); 43 } 44 a[++atot] = k -> x; 45 if (k -> right != NULL) { 46 left(k -> right); 47 } 48 } 49 void right(BN* k) { 50 if (k -> right != NULL) { 51 right(k -> right); 52 } 53 b[++btot] = k -> x; 54 if (k -> left != NULL) { 55 right(k -> left); 56 } 57 } 58 int isSymmetric(BN* k) { 59 if (k == NULL) { 60 return 0; 61 } 62 if ((k -> left == NULL)&&(k -> right == NULL)) { 63 return 1; 64 } 65 left(k -> left); 66 right(k -> right); 67 int i; 68 for (i = 1; i <= 50; i++) { 69 if (a[i] != b[i]) { 70 return 0; 71 } 72 } 73 return 1; 74 } 75 int main() { 76 BN* root = NULL; 77 buildTree(&root); 78 printf("%d\n", isSymmetric(root)); 79 freeTree(root); 80 return 0; 81 }
1 //symmetric.h 2 3 typedef struct node { 4 int x; 5 struct node* left; 6 struct node* right; 7 }BN; 8 int a[100] = {0}, b[100] = {0}; 9 int atot = 0, btot = 0; 10 void left(BN* k) { 11 if (k -> left != NULL) { 12 left(k -> left); 13 } 14 a[++atot] = k -> x; 15 if (k -> right != NULL) { 16 left(k -> right); 17 } 18 } 19 void right(BN* k) { 20 if (k -> right != NULL) { 21 right(k -> right); 22 } 23 b[++btot] = k -> x; 24 if (k -> left != NULL) { 25 right(k -> left); 26 } 27 } 28 int isSymmetric(BN* k) { 29 if (k == NULL) { 30 return 0; 31 } 32 if ((k -> left == NULL)&&(k -> right == NULL)) { 33 return 1; 34 } 35 left(k -> left); 36 right(k -> right); 37 int i; 38 for (i = 1; i <= 50; i++) { 39 if (a[i] != b[i]) { 40 return 0; 41 } 42 } 43 return 1; 44 }
标程
1.#include<stdio.h> 2.#include<stdlib.h> 3.#include"symmetric.h" 4.#define MAXD 50 5.void buildTree(BN** root) { 6. int temp; 7. BN** que[MAXD]; 8. int head = 0; 9. int tail = 1; 10. que[0] = root; 11. scanf("%d", &temp); 12. while (temp != -1) { 13. *que[head] = malloc(sizeof(struct node)); 14. (*que[head])->x = temp; 15. que[tail] = &((*que[head])->left); 16. tail = (tail + 1) % MAXD; 17. que[tail] = &((*que[head])->right); 18. tail = (tail + 1) % MAXD; 19. head = (head + 1) % MAXD; 20. scanf("%d", &temp); 21. } 22. while (head != tail) { 23. *que[head] = NULL; 24. head = (head + 1) % MAXD; 25. } 26.} 27.void freeTree(BN* root) { 28. if (root != NULL) { 29. freeTree(root->left); 30. freeTree(root->right); 31. free(root); 32. } 33.} 34.int main() { 35. BN* root = NULL; 36. buildTree(&root); 37. printf("%d\n", isSymmetric(root)); 38. freeTree(root); 39. return 0; 40.}
1.#ifndef SYMM 2.#define SYMM 3.#include<stdio.h> 4.#include<string.h> 5.#include<stdlib.h> 6.typedef struct node { 7. int x; 8. struct node* left; 9. struct node* right; 10.} BN; 11.int f = 1; 12.void isSymmetric2(BN* left, BN* right) { 13. if (f == 1) { 14. if (left == NULL && right != NULL) 15. f = 0; 16. else if (left != NULL && right == NULL) 17. f = 0; 18. else if (left != NULL && right != NULL) { 19. isSymmetric2(left->left, right->right); 20. isSymmetric2(left->right, right->left); 21. if (left->x != right->x) 22. f = 0; 23. } 24. } 25.} 26.int isSymmetric(BN* root) { 27. if (root == NULL) f = 0; 28. else isSymmetric2(root->left, root->right); 29. return f; 30.} 31.#endif
解释
标答的更好,因为同时判断了结构上面也是一个对称二叉树,我的有一点投机取巧,就是把根的左节点的左序遍历遍历了一遍,根的右节点的右序遍历遍历了一遍,判断两者相等则是对称,但是这不是充要条件,只是必要条件,侥幸过关==