C语言二叉树对称判断程序输入异常问题求助
二叉树对称判断程序的输入问题修复
问题
我想用C语言编写一个函数,输入二叉树后判断它是否中心对称。但当前程序无法正确接收输入,运行时main函数里不响应用户输入请求,而且始终不会询问右子树的输入。
原代码
#include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Structure for binary tree node struct Node { int data; struct Node *left; struct Node *right; }; // Function to create a new binary tree node from a given data. struct Node *newNode(int data) { struct Node *node = (struct Node *)malloc(sizeof(struct Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } // Helper function to check if two subtrees are mirror images of each other. bool isMirror(struct Node *leftSubtree, struct Node *rightSubtree) { if (leftSubtree == NULL && rightSubtree == NULL) return true; if (leftSubtree == NULL || rightSubtree == NULL) return false; return (leftSubtree->data == rightSubtree->data) && isMirror(leftSubtree->left, rightSubtree->right) && isMirror(leftSubtree->right, rightSubtree->left); } // Function to determine if the tree is symmetric. bool isSymmetric(struct Node *root) { if (root == NULL) return true; return isMirror(root->left, root->right); } // Function to build a binary tree based on user input. struct Node *buildTree() { int data; printf("Enter data (-1 for NULL): "); scanf("%d", &data); if (data == -1) return NULL; struct Node *newNodePtr = newNode(data); printf("Enter left child of %d: ", data); newNodePtr->left = buildTree(); printf("Enter right child of %d: ", data); newNodePtr->right = buildTree(); return newNodePtr; } int main() { struct Node *root = NULL; printf("Enter the root data: "); root = buildTree(); if (isSymmetric(root)) printf("The binary tree is symmetric. "); else printf("The binary tree is not symmetric. "); return 0; }
问题原因
程序的核心问题是字符串中的换行符错误:所有printf语句里的换行被写成了 (缺少反斜杠),这不是C语言的合法转义字符,会导致输出混乱,进而影响输入交互逻辑——系统无法正确解析输出提示,用户输入也会因为输出异常出现缓冲区问题,最终表现为不询问右子树输入、不响应用户请求。
修复后的代码
#include <stdbool.h> #include <stdio.h> #include <stdlib.h> // Structure for binary tree node struct Node { int data; struct Node *left; struct Node *right; }; // Function to create a new binary tree node from a given data. struct Node *newNode(int data) { struct Node *node = (struct Node *)malloc(sizeof(struct Node)); node->data = data; node->left = NULL; node->right = NULL; return node; } // Helper function to check if two subtrees are mirror images of each other. bool isMirror(struct Node *leftSubtree, struct Node *rightSubtree) { if (leftSubtree == NULL && rightSubtree == NULL) return true; if (leftSubtree == NULL || rightSubtree == NULL) return false; return (leftSubtree->data == rightSubtree->data) && isMirror(leftSubtree->left, rightSubtree->right) && isMirror(leftSubtree->right, rightSubtree->left); } // Function to determine if the tree is symmetric. bool isSymmetric(struct Node *root) { if (root == NULL) return true; return isMirror(root->left, root->right); } // Function to build a binary tree based on user input. struct Node *buildTree() { int data; printf("Enter data (-1 for NULL): "); // 清除输入缓冲区残留的换行符,避免后续输入异常 while (getchar() != '\n'); scanf("%d", &data); if (data == -1) return NULL; struct Node *newNodePtr = newNode(data); printf("Enter left child of %d:\n", data); newNodePtr->left = buildTree(); printf("Enter right child of %d:\n", data); newNodePtr->right = buildTree(); return newNodePtr; } int main() { struct Node *root = NULL; printf("Enter the root data:\n"); root = buildTree(); if (isSymmetric(root)) printf("The binary tree is symmetric.\n"); else printf("The binary tree is not symmetric.\n"); return 0; }
修复说明
- 把所有
printf语句中的替换为C语言合法的换行转义符\n,确保提示信息正确输出。 - 在
buildTree的scanf前添加while (getchar() != '\n');,清除输入缓冲区中残留的换行符,避免因用户输入回车导致后续scanf读取异常。
内容的提问来源于stack exchange,提问作者Daredevil
相关产品推荐
相关产品推荐

