当前位置:网站首页>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));
}
}
我吐了,这题好恶心。
一开始的思路是把这棵树层序遍历,每层结果左右相互比较,结果卡住了。错一下午。
最后变换思路,先把右子树翻转,再左右子树分别遍历比较。
边栏推荐
- Redis cluster view the slots of each node
- Objective C attribute keyword
- Es network layer
- [csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
- Flink学习6:编程模型
- AAAI2022 | Word Embeddings via Causal Inference: Gender Bias Reducing and Semantic Information Preserving
- Getting started with the go language is simple: go implements the Caesar password
- Smart subway | cloud computing injects wisdom into urban subway transportation
- [paddleseg source code reading] paddleseg custom data class
- Katalon框架测试web(二十六)自动发邮件
猜你喜欢
【罗技】m720
Global exposure and roller shutter exposure of industrial cameras
The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
Detailed explanation of PPTC self recovery fuse
Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
There is a problem that the package cannot be parsed in the like project
指针数组和数组指针
Confession code collection, who says program apes don't understand romance
*. No main manifest attribute in jar
随机推荐
ctf-pikachu-XSS
JDBC advanced
Introduction to asynchronous task capability of function calculation - task trigger de duplication
Spa in SDP
02 ls 命令的具体实现
Tcpclientdemo for TCP protocol interaction
【微服务|openfeign】feign的两种降级方式|Fallback|FallbackFactory
[webrtc] M98 Ninja build and compile instructions
pytest多进程/多线程执行测试用例
Why is the probability of pod increasing after IPtable
Katalon框架测试web(二十一)获取元素属性断言
Detailed explanation of PPTC self recovery fuse
Two commonly used graphics can easily realize data display
Activiti7 task service - process variables (setvariable and setvariablelocal)
LevelDB源码解读-SkipList
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
【webrtc】m98 ninja 构建和编译指令
Select sorting and bubble sorting template
Small record of thinking
支持首次触发的 Go Ticker