(轴对称二叉树)Binary Tree

本文介绍了一种通过先序遍历判断二叉树是否为轴对称的方法,并提供了具体的C++实现代码。通过对左右子树进行特定遍历,可以高效地判断二叉树是否关于中心轴对称。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In the computer world, there are various interesting trees and forests. One of the most earthy tree is binary tree.
Now Mosu has a problem, how to quickly judge a binary tree whether is symmetry.
Input There are multiple test cases. Each case contains one string, which represent the level order of a binary tree. The value of nodes are integer, and always exist a space after a value in the string, the “null” indicate this leaf node not exist.
Output If the tree is symmetry, output 1. If not, in the first line output 0, then output the preorder traversal of the binary tree on the second line.
Sample Input
6 2 2 3 4 4 3
1 2 2 null 3 null 3
Sample Output
1
0
12323

题意:输入一串数,构成一棵二叉树,判断是否是对称二叉树,如果是轴对称的二叉树,那么输出1
否则输出0,并且输出先序遍历这课二叉树的结果。

解法:先序遍历左右子树,如果结果相等那么就是对称的,否则不对称。
对左子树: ( p , l , r )
对右子树: ( p , r , l )

PS:校赛题留念

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x80000000;
const double PI=acos(-1);
const int N=1e10;

class binary_Tree{
public:
    int val;
    binary_Tree * left, * right;
    binary_Tree(){
        val=0,left=NULL,right=NULL;
    }
};

queue<int> ans;
void PRE_DFS(binary_Tree * now){
    if(now==NULL)
        return ;
    ans.push(now->val);
    PRE_DFS(now->left);
    PRE_DFS(now->right);
}

int main()
{
    string str;
    while(getline(cin,str)){

        //check
        int fla=1,ccnt=1,c=0;
        string s1="",s2="";

        stringstream ss;
        binary_Tree * root=NULL;
        ss<<str;
        string s;
        queue<binary_Tree *> que;
        int cnt=0;
        while(ss>>s){
            //check
            if(root!=NULL){
            if(c<ccnt/2){
                if(s=="null")
                    s1=s1+":";
                else
                    s1=s1+s;
                c++;
            }
            else{
                if(s=="null")
                    s2=":"+s2;
                else
                    s2=s+s2;
                c++;
            }
            }
            //--
            if(s!="null"){
                int x;stringstream sss;
                sss<<s;sss>>x;
                if(root==NULL){
                    binary_Tree * newnode = new(binary_Tree);
                    newnode->val=x;
                    root=newnode;
                    que.push(newnode);
                }
                else{

                    if(cnt%2==1){
                        binary_Tree * newnode = new(binary_Tree);
                        newnode->val=x;
                        que.front()->left=newnode;
                        que.push(newnode);
                    }
                    else{
                        binary_Tree * newnode = new(binary_Tree);
                        newnode->val=x;
                        que.front()->right=newnode;
                        que.push(newnode);
                        que.pop();
                    }
                }
            }
            cnt++;
            if(cnt==2*ccnt-1){

                if(s1!=s2){
                    fla=0;
                }
                s1=s2="";
                c=0;
                ccnt*=2;
            }
        }
        if(fla){
            cout<<1<<endl;
        }
        else{
            cout<<0<<endl;
            PRE_DFS(root);
            while(!ans.empty()){
                cout<<ans.front();
                ans.pop();
            }
            cout<<endl;
        }
    }
    return 0;
}

附数据


//input

1 2 2
1 null 2
1 null
1 -2 -2 3 4 4 3
1 2 2 3 4 4 3 null null 5 null null 5 null null
1 2 -2 3 4 4 3 null null 5 null null 5 null null
1 2 2 -3 4 4 -3 null null 5 null null 5 null null
13 -1178 -1178 -37 38 38 -37 null null null 2 2
13 -1178 -1178 -37 38 38 -37 null null null 2 2 null
13 -1178 -1178 -37 38 38 -37 null null null 2 -2
13 -1178 -1178 -37 38 38 -37 null null null 2 -2 null
1 2 2 null 3 null 3
1 2 2 null 3 null 3 null null null
1 2 2 null 3 3 null null null
-1 2 2 null 3 3 null null null
1
-1
0
1 2
-1 null null
1 2 2 -3 4 4 -3 5 -6 7 null null 7 -6 5 null null 8 9 null null null null 9 8 null null null 3 null -1000 -1000 null 3 null null null null -189 -189 null null null 3 -77 -77 3 null null null -10 -10 null null
-1 2 2 93333334 4 4 93333334 5 -6 7 null null 7 -6 5 null null 8 9 null null null null 9 8 null null null 3 null -1000 -1000 null 3 null null null null -189 -189 null null null 3 -77 -77 3 null null null -10 -10 null null
0 -0 0 1 null null 1 -0 0 0 0 null null null -1111 -1111
0 -0 0 1 -0 0 1
-100 0 -0 null 33 33 null -0 null null 0 10101 null null 10101 -0 -0 0 0 null null
1 2 2 -3 null null -3 null 4 4 null null 10 10 null -3 6 6 -3 null null null 0 -0 null null null null 1 1 null null 12 12 null null -0 0 null -0 -0 0 -0
-11 -22 -22 -5 null null -5 0 null null 0 133 null null 133 -3 -3


//output

1
0
12
1
1
1
0
12345-2453
1
1
1
0
13-1178-37382-117838-2-37
0
13-1178-37382-117838-2-37
0
12323
0
12323
1
1
1
1
1
0
12
1
1
1
1
1
1
1
0
-11-22-50133-3-3-22-50133


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值