Leetcode - Tree - 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

Note:

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

/**
 * 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) {
        if(root==NULL)
            return true;
        return isEqual(root->left, root->right);
    }
    bool isEqual(TreeNode *lhs, TreeNode *rhs)
    {
        if(!rhs&&!lhs)
            return true;
        if((lhs&&!rhs)||(!lhs&&rhs))
            return false;
        return (lhs->val==rhs->val)&&isEqual(lhs->left,rhs->right)&&isEqual(lhs->right,rhs->left);
    }
};


告别代码恐惧!用KRobot图形化编程,10分钟搞定Arduino巡线小车(附完整接线图) 本文介绍如何使用KRobot图形化编程工具快速搭建Arduino巡线小车,无需编写代码。从硬件准备到逻辑构建,详细讲解传感器数据处理、电机控制优化及高级功能扩展,帮助教育工作者和创客爱好者10分钟内完成项目。 阅读详情

相关推荐

VL53L0X测距不准?手把手教你做RefSPAD校准与环境补偿(附Arduino/STM32代码)

本文详细解析了VL53L0X激光测距传感器精度不足的原因,并提供了RefSPAD校准与环境补偿的完整解决方案。通过Arduino和STM32代码示例,手把手教你实现毫米级测距稳定性,适用于机器人避障、工业检测等场景。

weixin_28744423的博客 257

LeetCode 101. Symmetric Tree 题解

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

cannonjinx的博客 2008

微信自动化必备软件:inspect(获取微信界面元素)

SaveButton10015.2:1 ✅(合格)Passed ✅说明这个控件完全符合无障碍标准。

Ven%的博客 1382

闯关leetcode——101. Symmetric Tree

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

方亮的专栏 606

二进制部署的K8s集群,如何对到期证书更新

一年前搭建的k8s集群,突然不能够正常访问,出现502错误,首先怀疑容器没起来,先对问题进行排查,排查响应的容器有没有正常启动,状态一切正常,访问容器地址也可以正常访问,于是访问node port地址,发现无法正常访问,确定问题出现在svc中。 由于其他的应用、系统也无法提供正常的服务,应该是k8s的问题,非单个应用的原因。查看服务状态,kubectl describe svc xxx,endp...

时光旅行者 3771

读取rosbag中的IMU信息并转为tum格式的csv和txt

rostopic -b xx.bag -p /xx > xxx.txt #for euroc structvio import os, sys,re,csv name="street_07" # basedir="/media/yinjie/2498AF1F98AEEF0C/adataset/15results/imu_bagtxt/"+name+'.txt' # txtdir="/media/yinjie/2498AF1F98AEEF0C//adataset/15results/imu_o

qq_42928559的博客 1919

leetcode 之 Symmetric Tree 镜像树

Symmetric Tree

fangjian1204的专栏 2161

烧录代码显示:no st-link 和Targrt DLL has been cancelled,如何解决?

🏆 本文收录于《全栈Bug调优(实战版)》专栏,致力于分享我在项目实战过程中遇到的各类Bug及其原因,并提供切实有效的解决方案。无论你是初学者还是经验丰富的开发者,本文将为你指引出一条更高效的Bug修复之路,助你早日登顶,迈向财富自由的梦想🚀!同时,欢迎大家关注、收藏、订阅本专栏,更多精彩内容正在持续更新中。让我们一起进步,Up!Up!Up!    备注: 部分问题/难题源自互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验总结所得,数条可行方案供所需之人参考。

**My Coding Family** 1579

入坑LeetCode: 101. Symmetric Tree

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

chicwzh的博客 257

IM 系统设计:仿微信架构,Netty + Protobuf + WebSocket 如何支撑百万级长连接?

相比 JSON,Protobuf 的体积通常只有前者的 1/5 到 1/10,且解析速度快一个数量级。// 消息类型:1-登录,2-心跳,3-单聊,4-群聊,5-ACK// 发送方ID// 接收方ID// 消息内容// 时间戳// 消息唯一ID,用于去重和ACK。

BUG猿的博客 1378

【webrtc】openh264编码:动态配置码控参数触发IDR编码

SetRates 下发RateControlParameters 给到openh264 编码器 void SetRates(const RateControlParameters& parameters) override; LayerConfig 编码器这边的配置是layerconfig,具体到层的: std::vector<LayerConfig> configurations_; void H264EncoderImpl::SetRates(const RateCo.

突围 725

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

毕设加分项!手把手教你完成基于YOLOv8与PyQt5的多类别目标检测系统——让你的毕设从“完成”到“出色”

从KITTI数据集处理,到YOLOv8模型训练,再到PyQt5界面开发,整个流程其实没有想象中那么难。只要你跟着步骤一步步来,不仅能顺利完成毕设,还能掌握目标检测和GUI开发的核心技能——这些技能在未来的求职、读研中都非常实用。如果过程中遇到问题,别担心!可以去YOLOv8的官方文档、PyQt5论坛找答案,也可以和同学、老师交流。记住,完成毕设的过程也是你技术成长的过程,坚持下去,你会为自己的成果感到自豪。

m0_71589036的博客 179

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

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

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

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

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(二叉树)

题目来源: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力扣 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 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的专栏 2370

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 - Tree - Convert Sorted Array to Binary Search Tree
下一篇: 华为机试 - 大数之和
JerkWisdom
博客等级 码龄14年 70粉丝 76原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值