101. Symmetric Tree

本文介绍了一种检查二叉树是否为中心对称的方法,提供了递归与非递归两种算法实现。递归方法通过比较左右子树来判断对称性;非递归方法使用队列存储节点并进行逐层对比。

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

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

For example, this binary tree is symmetric:

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

 

But the following is not:

    1
   / \
  2   2
   \   \
   3    3

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

算法1:递归解法,判断左右两颗子树是否对称,只要两颗子树的根节点值相同,并且左边子树的左子树和右边子树饿右子树对称 且 左边子树的右子树和右边子树的左子树对称   

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        if(root == NULL)return true;
        return isSymmetricRecur(root->left, root->right);
    }
    bool isSymmetricRecur(TreeNode *root1, TreeNode *root2)
    {
        if(root1 != NULL && root2 != NULL)
        {
            if(root1->val == root2->val && 
                isSymmetricRecur(root1->left, root2->right) &&
                isSymmetricRecur(root1->right, root2->left))
                return true;
            else return false;
            
        }
        else if(root1 != NULL || root2 != NULL)
            return false;
        else return true;
        
    }
};
算法2:非递归解法,用两个队列分别保存左子树节点和右子树节点,每次从两个队列中分别取出元素,如果两个元素的值相等,则继续把他们的左右节点加入左右队列。要注意每次取出的两个元素,左队列元素的左孩子要和右队列元素的右孩子要同时不为空或者同时为空,否则不可能对称,同理左队列元素的右孩子要和右队列元素的左孩子也一样。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        if(root == NULL)return true;
        queue<TreeNode*> qleft, qright;
        if(root->left)qleft.push(root->left);
        if(root->right)qright.push(root->right);
        while(qleft.empty() == false && qright.empty() == false)
        {
            TreeNode *ql = qleft.front();
            TreeNode *qr = qright.front();
            qleft.pop();  qright.pop();
            if(ql->val == qr->val)
            {
                if(ql->left && qr->right)
                {
                    qleft.push(ql->left);
                    qright.push(qr->right);
                }
                else if(ql->left || qr->right)
                    return false;
                if(qr->left && ql->right)
                {
                    qleft.push(qr->left);
                    qright.push(ql->right);
                }
                else if(qr->left || ql->right)
                    return false;
            }
            else return false;
        }
        if(qleft.empty() && qright.empty())
            return true;
        else return false;
    }
};



内容概要:本文针对国内加密货币市场预测研究较少的现状,采用BP神经网络构建了CCi30指数预测模型。研究选取2018年3月1日至2019年3月26日共391天的数据作为样本,通过“试凑法”确定最优隐结点数目,建立三层BP神经网络模型对CCi30指数收盘价进行预测。论文详细介绍了数据预处理、模型构建、训练及评估过程,包括数据归一化、特征工程、模型架构设计(如输入层、隐藏层、输出层)、模型编译与训练、模型评估(如RMSE、MAE计算)以及结果可视化。研究表明,该模型在短期内能较准确地预测指数变化趋势。此外,文章还讨论了隐层节点数的优化方法及其对预测性能的影响,并提出了若干改进建议,如引入更多技术指标、优化模型架构、尝试其他时序模型等。 适合人群:对加密货币市场预测感兴趣的研究人员、投资者及具备一定编程基础的数据分析师。 使用场景及目标:①为加密货币市场投资者提供一种新的预测工具和方法;②帮助研究人员理解BP神经网络在时间序列预测中的应用;③为后续研究提供改进方向,如数据增强、模型优化、特征工程等。 其他说明:尽管该模型在短期内表现出良好的预测性能,但仍存在一定局限性,如样本量较小、未考虑外部因素影响等。因此,在实际应用中需谨慎对待模型预测结果,并结合其他分析工具共同决策。
执行 java -Djava.security.debug=provider -jar tks-shop-test.jar 报错[traceId:] 2025-06-03T09:48:27.798+08:00 WARN 311663 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tokenAes' defined in class path resource [com/yami/shop/common/config/ShopBeanConfig.class]: Failed to instantiate [cn.hutool.crypto.symmetric.AES]: Factory method 'tokenAes' threw exception with message: SecurityException: JCE cannot authenticate the provider BC [traceId:] 2025-06-03T09:48:27.810+08:00 DEBUG 311663 --- [ main] c.y.s.c.t.TraceThreadPoolTaskExceutor : Shutting down ExecutorService 'taskExecutor' [traceId:] 2025-06-03T09:48:27.841+08:00 INFO 311663 --- [lientSelector_1] RocketmqRemoting : closeChannel: close the connection to remote address[47.115.51.7:10911] result: true [traceId:] 2025-06-03T09:48:27.845+08:00 INFO 311663 --- [lientSelector_1] RocketmqRemoting : closeChannel: close the connection to remote address[47.115.51.7:9876] result: true [traceId:] 2025-06-03T09:48:27.911+08:00 INFO 311663 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat] [traceId:] 2025-06-03T09:48:27.960+08:00 INFO 311663 --- [ main] .s.b.a.l.ConditionEvaluationReportLogger : Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. [traceId:] 2025-06-03T09:48:28.016+08:00 ERROR 311663 --- [ main] o.s.boot.SpringApplication : Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tokenAes' defined in class path resource [com/yami/shop/common/config/ShopBeanConfig.class]: Failed to instantiate [cn.hutool.crypto.symmetric.AES]: Factory method 'tokenAes' threw exception with message: SecurityException: JCE cannot authenticate the provider BC at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:659) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:493) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1332) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1162) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:941) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) at org.springframework.boot.SpringApplication.run(SpringApplication.java:310) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1304) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1293) at com.yami.shop.admin.WebApplication.main(WebApplication.java:25) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) at org.springframework.boot.loader.Launcher.launch(Launcher.java:95) at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:65) Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [cn.hutool.crypto.symmetric.AES]: Factory method 'tokenAes' threw exception with message: SecurityException: JCE cannot authenticate the provider BC at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:171) at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) ... 27 common frames omitted Caused by: 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 cn.hutool.crypto.symmetric.SymmetricCrypto.<init>(SymmetricCrypto.java:83) at cn.hutool.crypto.symmetric.AES.<init>(AES.java:50) at com.yami.shop.common.config.ShopBeanConfig.tokenAes(ShopBeanConfig.java:27) at com.yami.shop.common.config.ShopBeanConfig$$SpringCGLIB$$0.CGLIB$tokenAes$3(<generated>) at com.yami.shop.common.config.ShopBeanConfig$$SpringCGLIB$$2.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:258) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) at com.yami.shop.common.config.ShopBeanConfig$$SpringCGLIB$$0.tokenAes(<generated>) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139) ... 28 common frames omitted Caused by: java.lang.SecurityException: JCE cannot authenticate the provider BC at java.base/javax.crypto.Cipher.getInstance(Cipher.java:722) at cn.hutool.crypto.SecureUtil.createCipher(SecureUtil.java:1032) ... 46 common frames omitted Caused by: java.lang.IllegalStateException: zip file closed at java.base/java.util.zip.ZipFile.ensureOpen(ZipFile.java:839) at java.base/java.util.zip.ZipFile.getManifestName(ZipFile.java:1065) at java.base/java.util.zip.ZipFile$1.getManifestName(ZipFile.java:1108) at java.base/javax.crypto.JarVerifier.verifySingleJar(JarVerifier.java:464) at java.base/javax.crypto.JarVerifier.verifyJars(JarVerifier.java:320) at java.base/javax.crypto.JarVerifier.verify(JarVerifier.java:263) at java.base/javax.crypto.ProviderVerifier.verify(ProviderVerifier.java:130) at java.base/javax.crypto.JceSecurity.verifyProvider(JceSecurity.java:190) at java.base/javax.crypto.JceSecurity.getVerificationResult(JceSecurity.java:218) at java.base/javax.crypto.Cipher.getInstance(Cipher.java:718) ... 47 common frames omitted
06-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值