当前位置:网站首页>LeetCode-101-对称二叉树
LeetCode-101-对称二叉树
2022-06-25 18:29:00 【z754916067】
题目

思路
- 反正是二叉树,先想递归吧,看能不能用递归做出来。
- 感觉做不出,又想到能不能用遍历做,先遍历到数组里,如果是对称的话应该会符合一定的规律?
- 中序遍历很适合,看数组是否对于根节点对称即可。
- 因为会出现根节点和左右子树相同的情况,所以对于null,也需要进行特殊处理,范围在[-100,100],将其视为101即可。
代码
ArrayList<Integer> ll = new ArrayList<>();
public boolean isSymmetric(TreeNode root) {
//首先对二叉树进行中序遍历 将数字加入ll中
InOrder(root);
//找到根节点的位置 查找是否根据此位置轴对称 如果不对称则返回false
//位置
int local = ll.indexOf(root.val);
//左右
int left=0,right = ll.size()-1;
while(left<=local || right>=local){
if(ll.get(left++)!=ll.get(right--)) return false;
}
return true;
}
public void InOrder(TreeNode root){
if(root.left!=null) InOrder(root.left);
if(root.left==null&&root.right!=null) ll.add(101);
ll.add(root.val);
if(root.right!=null) InOrder(root.right);
if(root.right==null&&root.left!=null) ll.add(101);
return;
}
边栏推荐
- [in depth understanding of tcapulusdb technology] business guide for creating doc acceptance
- Ruffian Heng embedded semimonthly issue 57
- C# asp,net core框架传值方式和session使用
- 【深入理解TcaplusDB技术】单据受理之事务执行
- [in depth understanding of tcapulusdb technology] tcapulusdb model
- SVN介绍及使用总结
- Batch uploading of local jar packages to nexus private server
- TCP/IP 测试题(二)
- [deeply understand tcapulusdb technology] tmonitor module architecture
- [deeply understand tcapulusdb technology] create a game area for document acceptance
猜你喜欢
![Current situation of China's hydraulic cylinder industry in 2020 (with application fields, policies and regulations, supply and demand status and enterprise pattern) [figure]](/img/2e/439b5dce9634d4015430c9cf06c5de.jpg)
Current situation of China's hydraulic cylinder industry in 2020 (with application fields, policies and regulations, supply and demand status and enterprise pattern) [figure]
![Analysis on policy, output and market scale of China's natural gas hydrogen production industry in 2020 [figure]](/img/f2/ec0fe8bec503c8788d6d549845b95c.jpg)
Analysis on policy, output and market scale of China's natural gas hydrogen production industry in 2020 [figure]
![Analysis of global tea production, consumption and import and export trade: China's tea production ranks first in the world [figure]](/img/16/ab5056dd26cf7b1a761a92eea46e54.jpg)
Analysis of global tea production, consumption and import and export trade: China's tea production ranks first in the world [figure]

【深入理解TcaplusDB技术】 Tmonitor模块架构

如何快速关闭8080端口
![Analysis on market scale and supply of China's needle coke industry in 2020 [figure]](/img/79/6b08b62be8768484f548b6e18bd810.jpg)
Analysis on market scale and supply of China's needle coke industry in 2020 [figure]

【深入理解TcaplusDB技术】单据受理之创建游戏区

初探Oracle全栈虚拟机---GraalVM

【深入理解TcaplusDB技术】单据受理之事务执行

One article solves all search backtracking problems of Jianzhi offer
随机推荐
Training of long and difficult sentences in postgraduate entrance examination day87
初探Oracle全栈虚拟机---GraalVM
[deeply understand tcapulusdb technology] one click installation of tmonitor background
中金财富安全吗? 开户需要多久
Trample hole record -- a series of coincidences caused by thread pool rejection policy
[in depth understanding of tcapulusdb technology] tcapulusdb construction data
【深入理解TcaplusDB技术】单据受理之创建业务指南
mysql事务讲解
使用宝塔来进行MQTT服务器搭建
如何快速关闭8080端口
Huawei cloud SRE deterministic operation and maintenance special issue (the first issue)
[in depth understanding of tcapulusdb technology] business guide for creating doc acceptance
Analysis of China's road freight volume, market scale and competition pattern in 2020 [figure]
【深入理解TcaplusDB技术】如何实现Tmonitor单机安装
Analysis on development scale and development trend of China's night economy industry in 2021 [figure]
Training of long and difficult sentences in postgraduate entrance examination day92
【深入理解TcaplusDB技術】TcaplusDB業務數據備份
Training of long and difficult sentences in postgraduate entrance examination day84
C ASP, net core framework value transfer method and session use
Analysis on the development trend of China's intense pulsed light equipment industry in 2021: the market scale is growing, and the proportion of imported brands is large [figure]