当前位置:网站首页>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 .
边栏推荐
- [webrtc] M98 Ninja build and compile instructions
- leetcode刷题:二叉树06(对称二叉树)
- Idea configuration 360zip open by default -- external tools
- TCP-三次握手和四次挥手简单理解
- Common methods of threads
- Interpretation of leveldb source code skiplist
- Flink learning 7: application structure
- Configuration and hot update of nocturnal simulator in hbuildx
- [Logitech] m720
- 还原窗口位置的微妙之处
猜你喜欢

还原窗口位置的微妙之处

Pointer array and array pointer

【微服务|openfeign】@FeignClient详解

Activiti7 task service - process variables (setvariable and setvariablelocal)

Introduction to asynchronous task capability of function calculation - task trigger de duplication

I Build a simple microservice project

干货!基于GAN的稀有样本生成

User defined path and file name of Baidu editor in laravel admin

Flink learning 7: application structure

There is a problem that the package cannot be parsed in the like project
随机推荐
Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
Redis cluster view the slots of each node
2020 Bioinformatics | TransformerCPI
Confession code collection, who says program apes don't understand romance
STM32 external DHT11 display temperature and humidity
Unity draws the trajectory of pinball and billiards
Small record of thinking
【webrtc】m98 ninja 构建和编译指令
Rhcsa-- day one
Exercises in quantum mechanics
Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
Introduction to asynchronous task capability of function calculation - task trigger de duplication
SDP中的SPA
分布式系统:what、why、how
Flink学习7:应用程序结构
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
【罗技】m720
Epidemic strikes -- Thinking about telecommuting | community essay solicitation
支持首次触发的 Go Ticker
Tcpclientdemo for TCP protocol interaction