101. Symmetric Tree

二叉树对称性检查
本文介绍了一种检查二叉树是否为自身镜像的方法,即判断其是否围绕中心对称。通过递归和非递归两种方式,详细解析了如何比较二叉树的左子树和右子树,以确定树的整体对称性。

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

But the following [1,2,2,null,3,null,3] is not:

    1
   / \
  2   2
   \   \
   3    3

Note:
Bonus points if you could solve it both recursively and iteratively.

方法1:递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root==NULL)
            return true;   
        return isSymmetric(root->left, root->right);
    }
private:
     bool isSymmetric(TreeNode* lNode,TreeNode* rNode)
     {
         if(lNode!=NULL && rNode!=NULL)
         {
             if(lNode->val != rNode->val)
                 return false;
             bool lres = isSymmetric(lNode->left, rNode->right);
             bool rres = isSymmetric(lNode->right, rNode->left);
             return lres && rres;                           
         }
        else if (lNode ==NULL && rNode == NULL)
            return true;
        else
            return false;  
     }       
};

方法2:非递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root==NULL)
            return true;   
       queue<TreeNode*> nodes;
       nodes.push(root->left);
       nodes.push(root->right);
       while(!nodes.empty())
       {   
           int len = nodes.size();
           while(len)
           {
               TreeNode* nodeL = nodes.front();
               nodes.pop();
               TreeNode* nodeR = nodes.front();              
               nodes.pop();
               len=len-2;
               if(nodeL && nodeR)
               {
                   if(nodeL->val != nodeR->val)
                    return false;
               } 
               else if(!nodeL && !nodeR)
               {
                   continue;
               }
               else 
               {
                   return false;
               }
               nodes.push(nodeL->left);
               nodes.push(nodeR->right);
               nodes.push(nodeL->right);
               nodes.push(nodeR->left);             
           }          
       } 
        return true;
    } 

};

 

2025-09-01 17:23:58 INFO com.putian.datapush.presto.PrestoServiceImpl - cn.hutool.crypto.CryptoException: SecurityException: JCE cannot authenticate the provider BC at cn.hutool.crypto.SecureUtil.createCipher(SecureUtil.java:1034) at cn.hutool.crypto.CipherWrapper.<init>(CipherWrapper.java:39) at cn.hutool.crypto.symmetric.SymmetricCrypto.init(SymmetricCrypto.java:150) at cn.hutool.crypto.symmetric.SymmetricCrypto.<init>(SymmetricCrypto.java:127) at cn.hutool.crypto.symmetric.SymmetricCrypto.<init>(SymmetricCrypto.java:115) at cn.hutool.crypto.symmetric.SymmetricCrypto.<init>(SymmetricCrypto.java:104) at com.putian.common.secure.SecureUtil.sm4Encrypt(SecureUtil.java:1443) at com.putian.datapush.service.PushDataService.executeEncipher(PushDataService.java:452) at com.putian.datapush.service.PushDataService.encipher(PushDataService.java:407) at com.putian.datapush.service.PushDataService.makeUpData(PushDataService.java:360) at com.putian.datapush.service.PushDataService.processInBatches(PushDataService.java:307) at com.putian.PushDataJobStartor.main(PushDataJobStartor.java:35) Caused by: java.lang.SecurityException: JCE cannot authenticate the provider BC at javax.crypto.Cipher.getInstance(Cipher.java:662) at cn.hutool.crypto.SecureUtil.createCipher(SecureUtil.java:1032) ... 11 more Caused by: java.util.jar.JarException: file:/home/dataShared/job/putian-data-pushdata.jar has unsigned entries - cn/hutool/Hutool.class at javax.crypto.JarVerifier.verifySingleJar(JarVerifier.java:510) at javax.crypto.JarVerifier.verifyJars(JarVerifier.java:371) at javax.crypto.JarVerifier.verify(JarVerifier.java:297) at javax.crypto.JceSecurity.verifyProviderJar(JceSecurity.java:164) at javax.crypto.JceSecurity.getVerificationResult(JceSecurity.java:190) at javax.crypto.Cipher.getInstance(Cipher.java:658) ... 12 more 2025-09-01 17:24:02 ERROR com.putian.PushDataJobStartor - 数据推送任务运行失败! cn.hutool.crypto.CryptoException: SecurityException: JCE cannot authenticate the provider BC at cn.hutool.crypto.SecureUtil.createCipher(SecureUtil.java:1034) at cn.hutool.crypto.CipherWrapper.<init>(CipherWrapper.java:39) at cn.hutool.crypto.symmetric.SymmetricCrypto.init(SymmetricCrypto.java:150) at cn.hutool.crypto.symmetric.SymmetricCrypto.<init>(SymmetricCrypto.java:127) at cn.hutool.crypto.symmetric.SymmetricCrypto.<init>(SymmetricCrypto.java:115) at cn.hutool.crypto.symmetric.SymmetricCrypto.<init>(SymmetricCrypto.java:104) at com.putian.common.secure.SecureUtil.sm4Encrypt(SecureUtil.java:1443) at com.putian.datapush.service.PushDataService.executeEncipher(PushDataService.java:452) at com.putian.datapush.service.PushDataService.encipher(PushDataService.java:407) at com.putian.datapush.service.PushDataService.makeUpData(PushDataService.java:360) at com.putian.datapush.service.PushDataService.processInBatches(PushDataService.java:307) at com.putian.PushDataJobStartor.main(PushDataJobStartor.java:35) Caused by: java.lang.SecurityException: JCE cannot authenticate the provider BC at javax.crypto.Cipher.getInstance(Cipher.java:662) at cn.hutool.crypto.SecureUtil.createCipher(SecureUtil.java:1032) 这个问题的解决办法?
最新发布
09-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值