当前位置:网站首页>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));
}
}

我吐了,这题好恶心。
一开始的思路是把这棵树层序遍历,每层结果左右相互比较,结果卡住了。错一下午。
最后变换思路,先把右子树翻转,再左右子树分别遍历比较。
边栏推荐
- 函数计算异步任务能力介绍 - 任务触发去重
- Simple dialogue system -- text classification using transformer
- vue多级路由嵌套怎么动态缓存组件
- Huawei cloud Kunpeng engineer training (Guangxi University)
- hbuildx中夜神模拟器的配置以及热更新
- [Yugong series] go teaching course 002 go language environment installation in July 2022
- AAAI2022 | Word Embeddings via Causal Inference: Gender Bias Reducing and Semantic Information Preserving
- VIM add interval annotation correctly
- Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
- Spa in SDP
猜你喜欢

SQL語句加强練習(MySQL8.0為例)

Mitsubishi M70 macro variable reading Mitsubishi M80 public variable acquisition Mitsubishi CNC variable reading acquisition Mitsubishi CNC remote tool compensation Mitsubishi machine tool online tool

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

分布式系统:what、why、how

三年进账35.31亿,这个江西老表要IPO了

ctf-pikachu-XSS

ctf-pikachu-CSRF

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?
![[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability](/img/46/cb5a10ffe3fcdffb7da68dbaef5b1f.png)
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability

渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
随机推荐
Select sorting and bubble sorting template
Pandora IOT development board learning (HAL Library) - Experiment 6 independent watchdog experiment (learning notes)
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
MySQL one master multiple slaves + linear replication
【读书会第十三期】视频文件的封装格式
透过JVM-SANDBOX源码,了解字节码增强技术原理
Small record of thinking
'2'>' 10'==true? How does JS perform implicit type conversion?
华为云鲲鹏工程师培训(广西大学)
SDP中的SPA
Katalon框架测试web(二十六)自动发邮件
Getting started with the go language is simple: go implements the Caesar password
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
Objective-C member variable permissions
idea修改主体颜色
Pointer array and array pointer
Flink学习8:数据的一致性
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.
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
SQL statement strengthening exercise (MySQL 8.0 as an example)