当前位置:网站首页>Leetcode-101-symmetric binary tree
Leetcode-101-symmetric binary tree
2022-06-25 18:42:00 【z754916067】
subject

Ideas
- It's a binary tree anyway , Think about recursion first , See if you can do it recursively .
- I don't think I can do it , And I thought about whether I could do it with traversal , Go through the array first , If it is symmetrical, it should conform to certain rules ?
- Middle order traversal is very suitable for , See whether the array is symmetrical to the root node .
- Because the root node is the same as the left and right subtrees , So for null, Special treatment is also required , The scope is [-100,100], Treat it as 101 that will do .
Code
ArrayList<Integer> ll = new ArrayList<>();
public boolean isSymmetric(TreeNode root) {
// First, the binary tree is traversed in middle order Add numbers to ll in
InOrder(root);
// Find the location of the root node Find out whether it is axisymmetric according to this position If not, returns false
// Location
int local = ll.indexOf(root.val);
// about
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;
}
边栏推荐
- SVN介绍及使用总结
- 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]
- el-table高度自适应
- Training of long and difficult sentences in postgraduate entrance examination day84
- 【ELT.ZIP】OpenHarmony啃论文俱乐部—数据密集型应用内存压缩
- RMAN backup database_ Duplexing backup sets
- Regular expression summary
- TCP/IP 测试题(一)
- 跳一跳小游戏辅助(手动版本)py代码实现
- GenICam GenTL 标准 ver1.5(1)
猜你喜欢
![[elt.zip] openharmony paper Club - memory compression for data intensive applications](/img/54/8248c1d95b04498d44a00ea94a6c85.png)
[elt.zip] openharmony paper Club - memory compression for data intensive applications

削足适履 - 谈谈赛道上的坡道改造

【C语言练习——打印上三角及其变形(带空格版)】

GenICam GenTL 标准 ver1.5(1)

158_模型_Power BI 使用 DAX + SVG 打通制作商业图表几乎所有可能
![Overview and trend analysis of China's foreign direct investment industry in 2020 [figure]](/img/b3/73e01601885eddcd05b68a20f83ca8.jpg)
Overview and trend analysis of China's foreign direct investment industry in 2020 [figure]
![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]

华为发布两大昇腾计划 推动AI人才发展和科研创新
![[in depth understanding of tcapulusdb technology] new models of tcapulusdb](/img/10/f94a5e1ebeaa803c754dd77351950f.png)
[in depth understanding of tcapulusdb technology] new models of tcapulusdb

Regular expression summary
随机推荐
广州华锐互动VR全景为各行各业带来发展
El table highly adaptive
anaconda下载清华源
How to sort massive data? How to process data between memory and hard disk?
Problems encountered during the use of pychar
QQ机器人闪照转发/撤回消息转发【最新beta2版本】
【深入理解TcaplusDB技术】TcaplusDB机型
Under what circumstances do you need to manually write the @bean to the container to complete the implementation class
Analysis on employment compensation of 2021 college graduates: the average monthly starting salary of doctors, masters, undergraduates and junior colleges is 14823 yuan, 10113 yuan, 5825 yuan and 3910
网络安全检测与防范 测试题(一)
RMAN backup database_ Use RMAN for split mirror backup
TCP/IP 测试题(二)
Detailed explanation of route add command
GNU nano
两轮市场红海,利尔达芯智行如何乘风破浪?
JVM understanding
[in depth understanding of tcapulusdb technology] how to realize single machine installation of tmonitor
LeetCode 322. Change exchange & dynamic planning
PHP数据库连接version1.1
RMAN backup database_ Restart RMAN backup