当前位置:网站首页>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 .
边栏推荐
- JS实现文字滚动 跑马灯效果
- The maximum expiration time of client secret in azure ad application registration is modified to 2 years
- Select sorting and bubble sorting template
- Confession code collection, who says program apes don't understand romance
- (指針)自己寫一個比較字符串大小的函數,功能與strcmp類似。
- Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
- ctf-pikachu-XSS
- Tcpclientdemo for TCP protocol interaction
- ctf-pikachu-XSS
- Getting started with the go language is simple: go implements the Caesar password
猜你喜欢
Rhcsa-- day one
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
三菱M70宏变量读取三菱M80公共变量采集三菱CNC变量读取采集三菱CNC远程刀补三菱机床在线刀补三菱数控在线测量
[csrf-01] basic principle and attack and defense of Cross Site Request Forgery vulnerability
laravel admin里百度编辑器自定义路径和文件名
ctf-pikachu-XSS
【CSRF-01】跨站请求伪造漏洞基础原理及攻防
[Logitech] m720
How to telecommute more efficiently | community essay solicitation
Parameterization of controls in katalon
随机推荐
Pointer array and array pointer
Pandora IOT development board learning (HAL Library) - Experiment 6 independent watchdog experiment (learning notes)
leetcode刷题:二叉树07(二叉树的最大深度)
Why is the probability of pod increasing after IPtable
Graduation project: design seckill e-commerce system
Flink learning 6: programming model
三菱M70宏变量读取三菱M80公共变量采集三菱CNC变量读取采集三菱CNC远程刀补三菱机床在线刀补三菱数控在线测量
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Epidemic strikes -- Thinking about telecommuting | community essay solicitation
分布式系统:what、why、how
Parameterization of controls in katalon
Two commonly used graphics can easily realize data display
The three-year revenue is 3.531 billion, and this Jiangxi old watch is going to IPO
Flink learning 8: data consistency
干货!基于GAN的稀有样本生成
Idea modify body color
指针数组和数组指针
User defined path and file name of Baidu editor in laravel admin
Activiti7 task service - process variables (setvariable and setvariablelocal)
【微服务|openfeign】使用openfeign远程调用文件上传接口