Leetcode-101: Symmetric Tree

小样本学习概述 目录 前言 小样本学习存在的意义? 什么是小样本学习? 小样本学习的方法有哪些? 结语 前言 小样本学习(Few-Shot Learning)是近几年兴起的一个研究领域,小样本学习旨在解决在数据有限的机器学习任务。 小样本学习存在的意义? 近些年,以深度卷积神经网络为代表的深度学习方法在各类机器学习任务上取得了优异的成绩——很多任务上已经超越了人类表现。狂欢背后,危机四伏。因为这些深度学习方法work的关键之一是海量标注数据的支持。 但是在工业界,很多时候难以获得海量的训练数据,更. 阅读详情

这题也是经典题,需要注意。我的解法也是参考了网上的答案。
解法1: 递归

bool isMirror(TreeNode* r1, TreeNode* r2) {
    if (!r1 && !r2) return true;
    if (!r1 || !r2) return false;
    if (r1->val!=r2->val) return false;
    return isMirror(r1->left, r2->right) && isMirror(r1->right, r2->left);

}

bool isSymmetric(TreeNode* root) {

    if (!root) return true;
    return isMirror(root->left, root->right);
}

注意: isSymmetric()里面也可以不用if(),而是直接return isMirror(root, root),在leetcode里面这个不要if的version只要8秒,而我的version需要9秒,不知道为什么少了一次递归还慢些?

解法2:迭代。跟二叉树层次遍历差不多。

bool isSymmetric(TreeNode* root) {
    queue<TreeNode*> q;
    if (!root) return true;
    q.push(root->left);
    q.push(root->right);
    while(!q.empty()) {
        TreeNode* p1=q.front(); q.pop();
        TreeNode* p2=q.front(); q.pop();
        if (!p1 && !p2) continue;  //注意这里要返回。
        if (!p1 || !p2) return false;
        if (p1->val != p2->val) return false;
        q.push(p1->left);
        q.push(p2->right);
        q.push(p1->right);
        q.push(p2->left);
    }

    return true;
}

注意: 这里网上有的解法是一开始不检查if(!root),而是push(root)两次。我试了一下,这个version在leetcode上耗时10秒,我的上面的version只push(root)一次,耗时8秒。

STM32F4串口接收ATGM336H GPS数据,手把手教你解析NMEA协议并转换经纬度格式 本文详细介绍了如何使用STM32F4开发板通过串口接收ATGM336H GPS模块的NMEA协议数据,并解析GNRMC语句获取经纬度信息。内容涵盖硬件连接、NMEA协议深度解析、数据校验实现以及经纬度格式转换的数学原理与代码实现,为嵌入式开发者提供完整的GPS数据处理解决方案。 阅读详情

相关推荐

Qt5+OSG实战:如何用osgQOpenGLWidget在窗口中加载3D模型(附完整代码)

本文详细解析了如何在Qt5中使用osgQOpenGLWidget加载和渲染3D模型,涵盖环境配置、核心架构、性能优化及高级技巧。通过实战案例和完整代码,帮助开发者实现工业级3D模型渲染,提升Qt与OSG的整合效率。

weixin_42741930的博客 263

入坑LeetCode: 101. Symmetric Tree

入坑LeetCode: 101. Symmetric Tree 1、解题思路 判断一棵二叉树是否对称,其实就是递归判断,层层判断,将左二子与右儿子比较,将左二子的右儿子与右儿子的左二子比较,如果始终相同,则返回true。 2、代码 bool isymmetric(struct TreeNode* left,struct TreeNode* right){ if(left == NULL &...

chicwzh的博客 257

本地部署llama3Chinese

这通常意味着你的构建环境可能不完整或者没有正确配置。通常是由 Visual Studio 的 C++ 工具提供的。(大模型对错误的解析)错误信息表明在尝试构建一个项目时,在 Windows 上,

m0_49343639的博客 1432

leetcode 101 : Symmetric Tree

问题描述 java实现 //way1 递归 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ ...

zero 217

单目视觉定位:深入解析PNP算法在机器人导航中的实战应用

本文深入解析了PNP算法在机器人单目视觉定位中的核心原理与实战应用。通过拆解坐标变换四步,详细阐述了如何从二维图像点反求相机三维位姿。文章结合OpenCV的solvePnP函数,提供了算法选择、参数调优及结果后处理的实用指南,并针对机器人导航中的特征匹配、尺度不确定性等挑战,给出了鲁棒性优化与实时性平衡的解决方案。

weixin_29083649的博客 419

裂缝二维检测:裂缝的长度和宽度检测

这一节,我们需要去整合一下前面的代码,在进行长度和宽度的测量时,需要裂缝的类型、个数、面积的帮助。这一章是本专栏的核心部分。以上代码我均试过,由于本人能力有限,看不懂代码,最终还是没有采用,使用的是最普通检测宽度的方法,即求得骨架总长度后,用总面积求取平均宽度。

m0_62919535的博客 4238

leetcode:101. Symmetric Tree

敢不敢把问题理清楚点???懒,写的啥破玩意儿,,,,回来继续写 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NU...

hunter 189

C++ Qt6 qml CMakeLists.txt如何添加qrc文件

本文介绍了在Qt6项目中如何通过CMake管理QML图片资源。相比Qt5的qmake,CMake需要手动创建qrc文件并配置CMakeLists.txt。关键步骤包括:1)创建resources.qrc文件并添加图片资源;2)在CMakeLists.txt中设置CMAKE_AUTORCC为ON开启自动资源处理;3)使用qt_add_resources将qrc文件绑定到项目。配置完成后,可通过"qrc:alias"方式引用资源图片,解决了相对路径管理复杂的问题。

琅嬛福地 1453

LeetCode 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 ...

forever_zzx的专栏 202

MediaPipe 从入门到精通:安装、配置与多场景应用实战手册

人体姿态检测是 MediaPipe 的核心功能之一,它能够从图像或视频中识别人体姿态,并输出关键点的坐标。这些关键点覆盖了身体的主要部位,例如肩膀、肘部、髋部、膝盖等,总计 33 个点。每个关键点包含 x、y、z 坐标(归一化到 0-1 范围)以及可见性评分,可用于分析动作、姿势或开发健身、游戏等应用。手部关键点检测是 MediaPipe 的另一大亮点,它可以识别图像或视频中的手部,并输出 21 个关键点的坐标。这些关键点覆盖手掌和手指的各个部位,适用于手势识别、虚拟现实或交互式应用。

m0_71589036的博客 389

【小白深度教程 1.3】使用 BP-Net 深度补全网络,进行 KITTI 稠密点云和图像融合(Depth Completion 实战)

在上一节中,我们展示了如何将从激光雷达获取的稀疏点云(深度)映射到图像。因此这篇论文我们尝试使用 Depth Completion(深度补全)技术,来将稀释的点云深度,补全为稠密深度。

2401_87064292的博客 1198

Leetcode 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: But the following [1,2,2,null,3,null,3] is n...

qq_34314736的博客 143

Mamba SSM 实战:IMDb 文本分类 + Mamba-ssm 全流程

Mamba SSM 文本分类链路:IMDb 二分类 → MambaLMHeadModel (d_model=256, n_layer=8, d_state=16, exp2=2) → 末 token 分类头。5 epoch 达 89.2% 准确率 (与 BERT-base 持平), 但显存降低 44% (3.5GB vs 6.2GB)。建议序列长度 ≥ 1024 再选 Mamba, 短序列 (< 512) 用 BERT 更高效。扩展时注意d_state。

m0_71589036的博客 57

leetcode[101]:Symmetric Tree

Symmetric TreeGiven a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).Note: Bonus points if you could solve it both recursively and iteratively./** * Definition

hahubaba的博客 448

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

LeetCode 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 is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the f

judadeshu的博客 252

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

leetcode 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,

永远不要给自己找借口 352

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

LeetCode : 101. Symmetric Tree

LeetCode 101 对称树 比较简单的题目 利用递归-分治方法解决就ok了(虽然递归很占内存...) 直接看代码吧public int depth(TreeNode root) { if(root == null) { return 0; }else { int leftLength = depth(root.

JZ(Bao哥)的Galaxy 400

LeetCode: 101. Symmetric Tree

LeetCode: 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: ...

杨领well的专栏 739

Leetcode: 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 B...

liveway6的博客 159

LeetCode101: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 ...

卷卷萌的博客 217

Leetcode学习笔记:#101. Symmetric Tree

Leetcode学习笔记:#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...

ccystewart的博客 241

LeetCode: Symmetric Tree [101]

【题目】 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 No

Harry Huang 1161

Leetcode 101: Symmetric Tree

问题描述: Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). 思路: (1)若根节点不存在,则以其为根的子树不存在,则认为是对称 (2)若根节点存在,且 (1) 左子树和右子树均不存在,认为是对称 (2) 左子树和右子树仅存其一,认为是不对称 (3) 左子树与右子树均存在 (1)值相等, 递归比较 (2)值不相等,则不对称 class Sol

weixin_51401145的博客 122

LeetCode笔记:101. Symmetric Tree

判断一个二叉树是不是镜像对称的

Minecraft 710

leetcode 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 is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the following is not:

a253664942的专栏 495

Leetcode:101. Symmetric Tree(JAVA)

【问题描述】 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

莫名的程序猿 730
上一篇: Leetcode-142: Linked List Cycle II
下一篇: LintCode 69: Binary Tree Level Order Traversal 按层次遍历二叉树 (Leetcode-102)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值