文章

55

粉丝

317

获赞

160

访问

32.9k

头像
判断二叉树是否对称 题解:第一步根据完全二叉树的性质递归建树,第二步递归判定树是否镜像对称
P1551 东北大学机试题
发布于2025年3月13日 23:47
阅读数 196

#include<iostream>
#include<string>
using namespace std;

struct TNode 
{
    char data;
    TNode* rchild, * lchild;


};
void BuildTree(string str,int index, struct  TNode* &root)
{
    if (index >= str.size()) return;
    if (str[index] == '#') { root = NULL;return; }
    root = new struct TNode;
    root->data = str[index];
    root->lchild = NULL;
    root->rchild = NULL;
    BuildTree(str, index * 2, root->lchild);
    BuildTree(str, index * 2+1, root->rchild);
    return;
}
//用于判断是否对称
bool Judge(TNode* lchild, TNode* rchild)
{
    if (!lchild && !rchild)return true;
    if (lchild && rchild) return Judge(lchild->lchild, rchild->rchild) && Judge(lchild->rchild, rchild->lch...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发