当前位置:网站首页>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;
}
边栏推荐
- LeetCode 198. Looting & dynamic planning
- 网络安全检测与防范 测试题(五)
- Dell R530内置热备盘状态变化说明
- . Net worker service adding a serial log record
- Overview and trend analysis of China's foreign direct investment industry in 2020 [figure]
- [in depth understanding of tcapulusdb technology] tcapulusdb regular documents
- 【深入理解TcaplusDB技术】TcaplusDB机型
- Current situation and trend analysis of China's glass packaging containers in 2021: the revenue of glass packaging containers increases year by year [figure]
- Leetcode force buckle (Sword finger offer 26-30) 26 Substructure of tree 27 Image of binary tree 28 Symmetric binary tree 29 Print matrix 30 clockwise Stack containing min function
- Lazy singleton mode from shallow to deep
猜你喜欢
Analysis on planting area, output and import of sugarcane in Guangxi in 2021: the output of sugarcane in Guangxi accounts for 68.56% of the total output of sugarcane in China [figure]
[in depth understanding of tcapulusdb technology] tcapulusdb model
[deeply understand tcapulusdb technology] table management of document acceptance
One article solves all search backtracking problems of Jianzhi offer
【深入理解TcaplusDB技术】 Tmonitor模块架构
《痞子衡嵌入式半月刊》 第 57 期
Problems encountered during the use of pychar
【深入理解TcaplusDB技术】单据受理之建表审批
Regular expression summary
158 Bar _ Modèle Power bi utilise Dax + SVG pour créer des diagrammes d'affaires presque toutes les possibilités
随机推荐
网络安全检测与防范 测试题(四)
Analysis on planting area, output and import of sugarcane in Guangxi in 2021: the output of sugarcane in Guangxi accounts for 68.56% of the total output of sugarcane in China [figure]
connect to address IP: No route to host
Ruffian Heng embedded semimonthly issue 57
C generic class case
C#泛型类案例
《痞子衡嵌入式半月刊》 第 57 期
RMAN backup database_ catalogue
Idea annotation color modification method (clear)
Pycharm 使用过程中碰到问题
GenICam GenTL 标准 ver1.5(1)
Addition, deletion, modification and query of mysql~ tables (detailed and easy to understand)
Lazy singleton mode from shallow to deep
解决sublime Text3 package control 无法安装插件问题
One article solves all search backtracking problems of Jianzhi offer
C ASP, net core framework value transfer method and session use
Development status of China's hydrotalcite industry in 2020 and analysis of major enterprises: the market scale is rapidly increasing, and there is a large space for domestic substitution [figure]
[elt.zip] openharmony paper Club - memory compression for data intensive applications
将Graph Explorer搬上JupyterLab:使用GES4Jupyter连接GES并进行图探索
Training of long and difficult sentences in postgraduate entrance examination day81