[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

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

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

我采用的是层次遍历的思想

class Solution {
public:
	bool isSymmetric(TreeNode *root) {
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		TreeNode **array=new TreeNode*[10000];
		TreeNode **array1=new TreeNode*[10000];
		memset(array,NULL,sizeof(TreeNode*)*10000);
		memset(array1,NULL,sizeof(TreeNode*)*10000);
		int count1=0,count2=0;
		if(!root)
			return true;
		_leftleveltraverse(root,array,count1);
		_rightleveltraverse(root,array1,count2);
		if(count1!=count2)
			return false;
		for(int i=0 ; i<count1 ; i++){
			if(!array[i] && !array1[i])
				continue;
			if(array[i] && array1[i] && array[i]->val==array1[i]->val)
				continue;
			else
				return false;
		}
		delete []array;
		delete []array1;
		return true;
	}
	void _leftleveltraverse(TreeNode* root, TreeNode **array, int &q){
		array[0]=root;
		int p=0;
		q=1;
		for(;p!=q;p++){
			if(array[p]==NULL)
				continue;
			array[q++]=array[p]->left;
			array[q++]=array[p]->right;
		}
	}
	void _rightleveltraverse(TreeNode* root, TreeNode **array, int &q){
		array[0]=root;
		int p=0;
		q=1;
		for(;p!=q;p++){
			if(array[p]==NULL)
				continue;
			array[q++]=array[p]->right;
			array[q++]=array[p]->left;
		}
	}
};
当然也可以使用递归的思想:

class Solution {
public:
    bool isSymmetric(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (root == NULL)
            return true;
        return check(root->left, root->right);
    }
    bool check(TreeNode *leftNode, TreeNode *rightNode) {
        if (leftNode == NULL && rightNode == NULL)
            return true;            
        if (leftNode == NULL || rightNode == NULL)
            return false;
            
        return leftNode->val == rightNode->val && check(leftNode->left, rightNode->right) && 
            check(leftNode->right, rightNode->left);
    }
};



HWDB1.1数据集处理全攻略:从gnt文件到PNG图片的完整转换教程 本文提供了HWDB1.1数据集处理的完整教程,详细解析了gnt二进制文件格式,并给出了从文件解析、标签映射到批量转换为PNG图片的Python脚本。教程涵盖了环境准备、跨平台解压方案,以及为PyTorch等深度学习框架准备数据的最佳实践,是进行手写汉字识别研究不可或缺的实用指南。 阅读详情

相关推荐

立创开源便携式多生理参数监护仪:基于STM32H743与ADS1292R的心电血氧体温监测系统设计

本文详细解析了基于STM32H743与ADS1292R的便携式多生理参数监护仪开源设计。项目集成了心电、血氧和体温监测功能,采用模块化硬件架构,并重点介绍了利用FIR滤波和Pan-Tompkins算法进行心电信号处理的核心技术。文章提供了完整的复刻指南与避坑要点,是学习医疗电子设备设计的优秀实践案例。

weixin_30600615的博客 327

LeetCode 101. Symmetric Tree 题解

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

cannonjinx的博客 2007

Ansys Electronics Desktop仿真——HFSS线圈寄生电阻,电感

Ansys Electronics Desktop仿真——HFSS线圈寄生电阻,电感

冲击10w 1万+

闯关leetcode——101. Symmetric Tree

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

方亮的专栏 606

告别龟速计算!用Python的fengwo模块5分钟搞定通达信COST/WINNER函数(附完整代码)

本文介绍如何使用Python的fengwo模块高效复现通达信的COST和WINNER函数,解决量化交易中的计算精度和性能问题。通过C++核心算法和多线程优化,计算速度提升显著,误差低于0.5%,并支持与通达信DLL无缝集成,适用于实盘交易策略开发。

weixin_42533751的博客 600

【Matlab/simulink/carsim】再生制动

【Matlab/simulink/carsim】再生制动

weixin_62311849的博客 621

leetcode 之 Symmetric Tree 镜像树

Symmetric Tree

fangjian1204的专栏 2161

学习STM32的无人机控制

下面是一个基于STM32的无人机控制的简单示例代码,包括四旋翼的PID控制、遥控器信号解析和电机控制。本示例以STM32F103系列为例,使用Keil C编译器进行开发。

qq_67153941的博客 706

入坑LeetCode: 101. Symmetric Tree

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

chicwzh的博客 257

Social-Auto-Upload实战:如何用Python+Playwright实现抖音/B站/小红书一键三连发?

本文详细介绍了如何使用Python和Playwright框架构建Social-Auto-Upload自动化上传工具,实现抖音、B站、小红书等平台的一键多发布。通过模拟真实用户操作和Cookie管理技术,该工具能有效解决内容创作者跨平台发布的重复劳动问题,提升工作效率。

day7的博客 1401

IDEA 2023.2最新破解安装教程(含详细配置步骤及常见问题解决)

本文详细介绍了IDEA 2023.2的安装流程及高效开发环境搭建指南,涵盖基础配置、项目结构优化、核心功能使用、插件推荐、性能优化等全方位内容,帮助开发者快速掌握这一强大的Java开发工具。

x2y3z4a5b的博客 1752

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

PX4从放弃到精通(二十七):固定翼姿态控制

固件版本 PX4 1.13.2欢迎交流学习,可加左侧名片。

超维空间科技的博客 6887

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

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

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笔记 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 力扣 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之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的专栏 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] Same Tree
下一篇: [leetcode] Binary Tree Level Order Traversal
和道一文字JC
博客等级 码龄19年 24粉丝 178原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值