当前位置:网站首页>Leetcode brush question: binary tree 06 (symmetric binary tree)
Leetcode brush question: binary tree 06 (symmetric binary tree)
2022-07-04 04:20:00 【Taotao can't learn English】
101. Symmetric binary tree
Given a binary tree , Check if it is mirror symmetric .
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. Symmetric binary tree **/
public class IsSymmetric {
public boolean isSymmetric(TreeNode root) {
// Only the root element , Directly through
if (root.left == null && root.right == null) {
return true;
}
/** * Flip the right half , Then the left and right sequences are traversed and compared */
if (root.left == null || root.right == null) {
return false;
}
// Zuo Zishu doesn't understand , Right subtree transformation
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);
// Come in again if you make a mistake
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));
}
}
I vomited. , This question is disgusting .
The initial idea is to traverse the tree sequence , The results of each layer are compared with each other , It got stuck . Wrong afternoon .
Finally, change your thinking , First flip the right subtree , Then traverse and compare the left and right subtrees respectively .
边栏推荐
- Spa in SDP
- [microservice openfeign] use openfeign to remotely call the file upload interface
- 02 ls 命令的具体实现
- Unity draws the trajectory of pinball and billiards
- [Yugong series] go teaching course 002 go language environment installation in July 2022
- PPt 教程,如何在 PowerPoint 中将演示文稿另存为 PDF 文件?
- Epidemic strikes -- Thinking about telecommuting | community essay solicitation
- Idea configuration 360zip open by default -- external tools
- Graduation project: design seckill e-commerce system
- RHCSA 04 - 进程管理
猜你喜欢
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
Idea configuration 360zip open by default -- external tools
Flink learning 6: programming model
laravel admin里百度编辑器自定义路径和文件名
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
RHCSA 04 - 进程管理
三菱M70宏变量读取三菱M80公共变量采集三菱CNC变量读取采集三菱CNC远程刀补三菱机床在线刀补三菱数控在线测量
ctf-pikachu-XSS
idea修改主体颜色
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
随机推荐
02 ls 命令的具体实现
函数计算异步任务能力介绍 - 任务触发去重
网络 - VXLAN
ctf-pikachu-CSRF
Flink learning 7: application structure
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
ROS2中CMake编译选项的设置
RHCSA 08 - automount配置
图解网络:什么是热备份路由器协议HSRP?
PostgreSQL users cannot create table configurations by themselves
There is a problem that the package cannot be parsed in the like project
如何远程办公更有效率 | 社区征文
Spa in SDP
Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
[book club issue 13] multimedia processing tool ffmpeg tool set
leetcode刷题:二叉树05(翻转二叉树)
Evolution of MySQL database architecture
VIM mapping command
ctf-pikachu-XSS
Getting started with the go language is simple: go implements the Caesar password