当前位置:网站首页>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 .
边栏推荐
- 【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
- RHCSA 07 - 用户与群组管理
- Katalon framework test web (XXVI) automatic email
- I was tortured by my colleague's null pointer for a long time, and finally learned how to deal with null pointer
- Interpretation of leveldb source code skiplist
- Unity移动端游戏性能优化简谱之 画面表现与GPU压力的权衡
- (指针)编写函数void fun(int x,int *pp,int *n)
- leetcode刷题:二叉树08(N叉树的最大深度)
- 1289_ Implementation analysis of vtask suspend() interface in FreeRTOS
- 批处理初识
猜你喜欢
2021 RSC | Drug–target affinity prediction using graph neural network and contact maps
Restore the subtlety of window position
Flink learning 6: programming model
软件测试是干什么的 发现缺陷错误,提高软件的质量
ctf-pikachu-CSRF
*. No main manifest attribute in jar
96% of the collected traffic is prevented by bubble mart of cloud hosting
Getting started with the go language is simple: go implements the Caesar password
LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MD_DynamicRelease”不匹配值“MDd_DynamicDebug”(main.obj 中)
Balance between picture performance of unity mobile game performance optimization spectrum and GPU pressure
随机推荐
[Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (I)
毕业三年,远程半年 | 社区征文
Pandora IOT development board learning (HAL Library) - Experiment 6 independent watchdog experiment (learning notes)
How was my life in 2021
02 specific implementation of LS command
如何远程办公更有效率 | 社区征文
Exercises in quantum mechanics
Support the first triggered go ticker
vim映射命令
"Implement both software and hardware" to help build a new cloud computing data center
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
PostgreSQL users cannot create table configurations by themselves
'2'>' 10'==true? How does JS perform implicit type conversion?
leetcode刷题:二叉树04(二叉树的层序遍历)
Distributed system: what, why, how
Idea configuration 360zip open by default -- external tools
LevelDB源码解读-SkipList
01 QEMU starts the compiled image vfs: unable to mount root FS on unknown block (0,0)
VIM add interval annotation correctly
批处理初识