当前位置:网站首页>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));
}
}
我吐了,这题好恶心。
一开始的思路是把这棵树层序遍历,每层结果左右相互比较,结果卡住了。错一下午。
最后变换思路,先把右子树翻转,再左右子树分别遍历比较。
边栏推荐
- 【微服务|openfeign】feign的两种降级方式|Fallback|FallbackFactory
- 华为云鲲鹏工程师培训(广西大学)
- Interpretation of leveldb source code skiplist
- Illustrated network: what is the hot backup router protocol HSRP?
- mysql数据库的存储
- Pytest multi process / multi thread execution test case
- 毕业设计:设计秒杀电商系统
- The maximum expiration time of client secret in azure ad application registration is modified to 2 years
- [paddleseg source code reading] paddleseg custom data class
- Confession code collection, who says program apes don't understand romance
猜你喜欢
Sales management system of lightweight enterprises based on PHP
AAAI2022 | Word Embeddings via Causal Inference: Gender Bias Reducing and Semantic Information Preserving
laravel admin里百度编辑器自定义路径和文件名
Getting started with the go language is simple: go implements the Caesar password
Parameterization of controls in katalon
渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
MySQL one master multiple slaves + linear replication
Flink学习6:编程模型
ctf-pikachu-XSS
Storage of MySQL database
随机推荐
Redis cluster uses Lua script. Lua script can also be used for different slots
Objective-C description method and type method
2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
Getting started with the go language is simple: go implements the Caesar password
Katalon中控件的参数化
My opinion on how to effectively telecommute | community essay solicitation
Value transfer communication between components (parent to child, child to parent, brother component to value)
User defined path and file name of Baidu editor in laravel admin
[paddleseg source code reading] paddleseg custom data class
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Graduation summary
Evolution of MySQL database architecture
Tcpclientdemo for TCP protocol interaction
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
Flink学习7:应用程序结构
深度优先搜索简要讲解(附带基础题)
vue多级路由嵌套怎么动态缓存组件
分布式系统:what、why、how
三菱M70宏变量读取三菱M80公共变量采集三菱CNC变量读取采集三菱CNC远程刀补三菱机床在线刀补三菱数控在线测量
LevelDB源码解读-SkipList