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

我吐了,这题好恶心。
一开始的思路是把这棵树层序遍历,每层结果左右相互比较,结果卡住了。错一下午。
最后变换思路,先把右子树翻转,再左右子树分别遍历比较。
边栏推荐
- Storage of MySQL database
- LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
- Activiti7 task service - process variables (setvariable and setvariablelocal)
- JDBC advanced
- Interpretation of leveldb source code skiplist
- Pointer array and array pointer
- Tcpclientdemo for TCP protocol interaction
- [book club issue 13] packaging format of video files
- Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
- SQL語句加强練習(MySQL8.0為例)
猜你喜欢

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

Detailed explanation of PPTC self recovery fuse

Pytest multi process / multi thread execution test case

PostgreSQL users cannot create table configurations by themselves

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

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

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?

函数计算异步任务能力介绍 - 任务触发去重

Restore the subtlety of window position

The difference between bagging and boosting in machine learning
随机推荐
SQL语句加强练习(MySQL8.0为例)
Brief explanation of depth first search (with basic questions)
Redis cluster view the slots of each node
10 reasons for not choosing to use free virtual hosts
Katalon框架测试web(二十一)获取元素属性断言
SDP中的SPA
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (I)
Objective C attribute keyword
Es network layer
Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
"Implement both software and hardware" to help build a new cloud computing data center
Perf simple process for multithreaded profile
【CSRF-01】跨站请求伪造漏洞基础原理及攻防
[Yugong series] go teaching course 002 go language environment installation in July 2022
How to dynamically cache components in Vue multi-level route nesting
My opinion on how to effectively telecommute | community essay solicitation
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
A review of reverse reinforcement learning at Virginia Tech (VT)
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
pytest多进程/多线程执行测试用例