You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

修复说明

  1. 把所有printf语句中的 替换为C语言合法的换行转义符\n,确保提示信息正确输出。
  2. 在buildTree的scanf前添加while (getchar() != '\n');,清除输入缓冲区中残留的换行符,避免因用户输入回车导致后续scanf读取异常。

内容的提问来源于stack exchange,提问作者Daredevil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 11:33:17