Leetcode | Symmetric Tree

leetcode之symmetric-tree(判断二叉树是否是镜像二叉树) leetcode之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: But the following ... 阅读详情

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

思路:对于根结点,若其不为空,则判断其左右子树是否都存在,且左右子树的根结点值必须相同,否则该树就不对称。然后继续判断这两颗左右子树是否对称。令左子树为L,右子树为R,需要做的就是判断L的左子树和R的右子树,他们的根结点值是否相同;还有L的右子树和R的左子树,他们的根结点值是否相同。(前提是左右子树必须都存在,如果有一方不存在,那么就不对称)。上述方法可以采用递归实现。

 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)
			return true;
		else 
			return judge(root->left,root->right);
    }
	bool judge(TreeNode* p, TreeNode* q)
	{
		if(!p && !q)
			return true;
		else if(p && q && p->val == q->val)
			return judge(p->left,q->right) && judge(p->right,q->left);
		else
			return false;
	}
};



免费替代Cursor!TRAE中文版这些隐藏功能90%的人不知道 本文深度评测了免费AI编程工具TRAE中文版,揭示其超越代码补全的隐藏功能。重点介绍了其SOLO模式如何重塑从需求到部署的全流程开发,以及对微信小程序、Taro、Ant Design Pro等国产技术栈的深度适配能力,为中文开发者提供了高度本地化的高效解决方案。 阅读详情

相关推荐

Excel曲线拟合实战:从基础趋势线到高级回归分析

本文系统介绍了Excel曲线拟合的完整工作流,从基础的散点图与趋势线应用,到利用Solver插件进行自定义高级回归分析。文章详细讲解了线性、多项式、指数、对数等六大常用数学模型的适用场景与选择方法,并强调了通过R²值、残差图评估拟合效果、避免过拟合与外推风险的关键步骤,帮助读者掌握从数据探索到预测评估的实战技能。

weixin_29280219的博客 533

闯关leetcode——101. Symmetric Tree

这题是检验树是否按中间线镜像。如果翻译成计算机语言,就是对比左子树中节点下的左子树和右子树中节点下的右子树是否一致。只有对比到最后一层都是nullptr时,才能说明子树一致,而数值或结构不一致都认为不一致。

方亮的专栏 606

Qt6安装保姆级教程:从国内镜像下载到配置避坑指南(附清华/中科大源)

本文提供详细的Qt6安装与配置指南,重点介绍如何利用清华大学、中国科学技术大学等国内镜像源解决下载速度慢的问题。内容涵盖在线安装器获取、组件选择策略、环境变量配置以及常见编译与部署问题的排查方法,旨在帮助国内开发者高效、顺畅地完成Qt6开发环境搭建。

weixin_29281915的博客 633

leetcode 之 Symmetric Tree 镜像树

Symmetric Tree

fangjian1204的专栏 2161

vos如何对接okcc(保姆级教程)

1.添加账户 点击 系统->账户管理 [图片] 2.创建费率 系统->费率管理 点击添加

qq_46333281的博客 3453

ChatUI vs Ant Design X 技术选型对比

维度ChatUI优势轻量级、易上手、完美兼容 Ant Design全能型框架、企业级扩展性、深度模型集成局限性功能单一、缺乏高级交互支持学习曲线陡峭、包体积较大最佳实践单独聊天窗口集成整体智能助手系统开发通过对比可见,ChatUI 适合快速实现基础对话功能,而Ant Design X 更适合构建复杂的 AI 驱动应用。实际选择时需结合项目规模、团队技术栈和对交互复杂度的需求综合评估。

四楼没电梯的专栏 2264

入坑LeetCode: 101. Symmetric Tree

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

chicwzh的博客 257

STGCN实战:从零构建PyTorch时空图卷积网络预测城市交通流

本文详细介绍了如何使用PyTorch从零构建时空图卷积网络(STGCN)进行城市交通流预测。通过将交通网络建模为图结构,并融合时间卷积与空间图卷积,STGCN能有效捕捉交通流的时空依赖性。文章提供了从数据预处理、核心模块(TimeBlock与STGCNBlock)搭建、模型训练到实战技巧的完整指南,帮助开发者快速掌握这一前沿的交通速度预测技术。

wasm7browser的博客 247

LeetCode 101. Symmetric Tree 题解

本题是二叉树的经典问题,主要考察对树的对称性的理解和遍历方法的应用。递归深度优先搜索:代码简洁易懂,逻辑清晰,是最直观的解决方案。迭代广度优先搜索:避免了递归的栈溢出问题,适用于深度较大的树。在实际应用中,递归方法通常是最首选的解决方案,因为它代码简洁且易于理解。但在处理深度较大的树时,迭代方法可能更为可靠。无论使用哪种方法,核心思想都是相同的:检查二叉树的左子树和右子树是否是镜像对称的。

cannonjinx的博客 2007

【Docker】常见容器问题解决#OCI runtime create failed: container_linux.go:349: starting container process

ERROR: for nginx Cannot start service nginx: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"rootfs_linux.go:58: mounting \\\"/data/docker-compose/n

liu_chen_yang的博客 5528

热力学仿真软件:ANSYS CFX_(15).旋转机械仿真

创建旋转域在中选择需要设置为旋转域的区域。选择选项。设置旋转速度在设置中,指定旋转速度(单位为弧度/秒)。也可以选择选项,并输入具体值。设置旋转轴在设置中,指定旋转轴的方向和位置。通常旋转轴的方向可以使用单位向量来表示,例如[0, 0, 1]表示沿Z轴旋转。旋转轴的位置可以通过指定一个点的坐标来确定,例如[0, 0, 0]表示旋转轴通过原点。旋转机械的仿真在ANSYS CFX中是一个复杂但重要的过程。

kkchenjj的博客 1158

leetcode 101. Symmetric Tree\100. Same Tree\572. Subtree of Another Tree

【代码】leetcode 101. Symmetric Tree\100. Same Tree\572. Subtree of Another Tree。

weixin_41554427的博客 525

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

LeetCode 101. Symmetric Tree

LeetCode 101. Symmetric Tree Solution1 参考《剑指offer》上的解法:https://blog.youkuaiyun.com/allenlzcoder/article/details/79769550 /** * Definition for a binary tree node. * struct TreeNode { * int val; * ...

Allenlzcoder的博客 353

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

LeetCode Top 100 Liked Questions 101. Symmetric Tree (Java版; Easy)

welcome to my blog LeetCode Top 100 Liked Questions 101. Symmetric Tree (Java版; Easy) 题目描述 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, ...

littlehaes的博客 149

LeetCode 力扣 101. 对称二叉树 symmetric tree DFS

大家觉得写还可以,可以点赞、收藏、关注一下吧! 也可以到我的个人博客参观一下,估计近几年都会一直更新!和我做个朋友吧!https://motongxue.cn 文章目录101. 对称二叉树进阶:分析提交结果 101. 对称二叉树 给定一个二叉树,检查它是否是镜像对称的。 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1

motongxue的博客 206

leetcode笔记 100. Same Tree & 101. Symmetric Tree

leetcode刷题14天 100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical and the nodes ha...

qq_29868413的博客 238

Leetcode之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 ...

qq_35455503的博客 203

LeetCode101—Symmetric Tree

LeetCode101—Symmetric Tree 判断一棵树是否对称

上善若水 769

LeetCode力扣 101. 对称二叉树 Symmetric Tree 题解代码 JavaScript

https://leetcode-cn.com/problems/symmetric-tree/ /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @return {boolean} */ function i

Coding home - 漂流瓶jz 460

LeetCode 101. Symmetric Tree(二叉树)

题目来源:https://leetcode.com/problems/symmetric-tree/ 问题描述 101.Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this b...

da_kao_la的博客 156

Leetcode Symmetric Tree Python 学习递归/DFS/BFS

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

润泽的博客 260

[leetcode]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

tuantuanls的专栏 690

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

Tingmei的专栏 2369

leetcode_symmetric-tree

题目链接 https://leetcode-cn.com/problems/symmetric-tree/ 题目内容 给定一个二叉树,检查它是否是镜像对称的。 例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 1 / 2 2 / \ / 3 4 4 3 但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1 / 2 2 \ 3 3 进阶: 你可以运用递归和迭代两种方法解决这个问题吗? 解题思路 1.若是空树或者是只有一个节点的树则返回tru

weixin_38554391的博客 175

LeetCode之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 followi

sunp823的专栏 342
上一篇: LeetCode | Balanced Binary Tree
下一篇: LeetCode | Same Tree
Dream_WHui
博客等级 码龄14年 32粉丝 72原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值