当前位置:网站首页>leetcode刷题:二叉树06(对称二叉树)
leetcode刷题:二叉树06(对称二叉树)
2022-07-04 03:51:00 【涛涛英语学不进去】
101. 对称二叉树
给定一个二叉树,检查它是否是镜像对称的。

package com.programmercarl.tree;
import java.util.*;
/** * @ClassName IsSymmetric * @Descriotion TODO * @Author nitaotao * @Date 2022/7/3 13:18 * @Version 1.0 * https://leetcode.cn/problems/symmetric-tree/ * 101. 对称二叉树 **/
public class IsSymmetric {
public boolean isSymmetric(TreeNode root) {
//只有根元素,直接通过
if (root.left == null && root.right == null) {
return true;
}
/** * 翻转右半段,然后左右层序遍历比较 */
if (root.left == null || root.right == null) {
return false;
}
//左子树不懂,右子树变换
reverse(root.right);
return getNum(root.left, root.right);
}
public boolean getNum(TreeNode root1, TreeNode root2) {
if ((root1 == null && root2 != null) || (root1 != null && root2 == null)) {
return false;
}
if (root1 == null && root2 == null) {
return true;
}
if (root1.val == root2.val) {
boolean left = getNum(root1.left, root2.left);
boolean right = getNum(root1.right, root2.right);
//有错再进来
return left && right;
} else {
return false;
}
}
public void reverse(TreeNode root) {
if (root == null) {
return;
}
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
reverse(root.left);
reverse(root.right);
}
public static void main(String[] args) {
TreeNode node15 = new TreeNode(6, null, null);
TreeNode node8 = new TreeNode(6, null, null);
TreeNode node7 = new TreeNode(5, null, node15);
TreeNode node4 = new TreeNode(5, node8, null);
TreeNode node3 = new TreeNode(4, null, node7);
TreeNode node2 = new TreeNode(4, node4, null);
TreeNode node1 = new TreeNode(3, node2, node3);
System.out.println(isSymmetric(node1));
}
}

我吐了,这题好恶心。
一开始的思路是把这棵树层序遍历,每层结果左右相互比较,结果卡住了。错一下午。
最后变换思路,先把右子树翻转,再左右子树分别遍历比较。
边栏推荐
- hbuildx中夜神模拟器的配置以及热更新
- Deep thinking on investment
- [paddleseg source code reading] paddleseg calculation dice
- 三年进账35.31亿,这个江西老表要IPO了
- EV6 helps the product matrix, and Kia is making efforts in the high-end market. The global sales target in 2022 is 3.15 million?
- idea修改主体颜色
- [Logitech] m720
- Go 语言入门很简单:Go 实现凯撒密码
- 01 QEMU starts the compiled image vfs: unable to mount root FS on unknown block (0,0)
- User defined path and file name of Baidu editor in laravel admin
猜你喜欢

Wechat official account web page authorization

深度优先搜索简要讲解(附带基础题)

Flink学习6:编程模型

指针数组和数组指针

Confession code collection, who says program apes don't understand romance

SQL语句加强练习(MySQL8.0为例)
![[Logitech] m720](/img/bb/44144a1c3907808398c05b3b36962c.png)
[Logitech] m720

2021 RSC | Drug–target affinity prediction using graph neural network and contact maps

Activiti7 task service - process variables (setvariable and setvariablelocal)

Go 语言入门很简单:Go 实现凯撒密码
随机推荐
Myslq delete followed by limit
Redis cluster uses Lua script. Lua script can also be used for different slots
[Logitech] m720
laravel admin里百度编辑器自定义路径和文件名
Pandora IOT development board learning (HAL Library) - Experiment 6 independent watchdog experiment (learning notes)
[paddleseg source code reading] paddleseg custom data class
Getting started with the go language is simple: go implements the Caesar password
JS实现文字滚动 跑马灯效果
Storage of MySQL database
深度优先搜索简要讲解(附带基础题)
Wechat official account web page authorization
vim映射命令
Pointer array and array pointer
The maximum expiration time of client secret in azure ad application registration is modified to 2 years
1289_FreeRTOS中vTaskSuspend()接口实现分析
ctf-pikachu-XSS
How was my life in 2021
渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
【罗技】m720