当前位置:网站首页>February 13, 2022-4-symmetric binary tree
February 13, 2022-4-symmetric binary tree
2022-07-05 23:01:00 【Procedural ape does not lose hair 2】
Give you the root node of a binary tree root , Check whether it is axisymmetric .
Example 1:
Input :root = [1,2,2,3,4,4,3]
Output :true
Example 2:
Input :root = [1,2,2,null,3,null,3]
Output :false
Tips :
The number of nodes in the tree is in the range [1, 1000] Inside
-100 <= Node.val <= 100
java Code :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
return check(root, root);
}
// Method 1 recursive
// private boolean check(TreeNode r1, TreeNode r2) {
// if(r1 == null && r2 == null) {
// return true;
// }
// if(r1 == null || r2 == null || r1.val != r2.val) {
// return false;
// }
// return check(r1.left, r2.right) && check(r1.right, r2.left);
// }
// Method 2 iteration
private boolean check(TreeNode r1, TreeNode r2) {
Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(r1);
q.offer(r2);
while ( !q.isEmpty()) {
r1 = q.poll();
r2 = q.poll();
if(r1 == null && r2 == null) {
continue;
}
if( r1== null || r2 == null || r1.val != r2.val) {
return false;
}
q.offer(r1.left);
q.offer(r2.right);
q.offer(r2.left);
q.offer(r1.right);
}
return true;
}
}
边栏推荐
- Selenium+Pytest自动化测试框架实战
- Douban scoring applet Part-2
- Distributed solution selection
- Binary tree (II) -- code implementation of heap
- PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
- Spectrum analysis of ADC sampling sequence based on stm32
- Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
- 一文搞定JVM的内存结构
- Editor extensions in unity
- One article deals with the microstructure and instructions of class
猜你喜欢
关于MySQL的30条优化技巧,超实用
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Three.JS VR看房
513. Find the value in the lower left corner of the tree
Spectrum analysis of ADC sampling sequence based on stm32
Nanjing: full use of electronic contracts for commercial housing sales
audiopolicy
My experience and summary of the new Zhongtai model
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
Codeforces Global Round 19
随机推荐
TCC of distributed solutions
Hcip day 12 (BGP black hole, anti ring, configuration)
Alibaba Tianchi SQL training camp task4 learning notes
Nail error code Encyclopedia
I closed the open source project alinesno cloud service
查看网页最后修改时间方法以及原理简介
Three.js-01 入门
Solve the problem of "no input file specified" when ThinkPHP starts
[untitled]
Editor extensions in unity
audiopolicy
一文搞定垃圾回收器
Composition of interface
Fix the memory structure of JVM in one article
Tensor attribute statistics
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
两数之和、三数之和(排序+双指针)
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
Masked Autoencoders Are Scalable Vision Learners (MAE)
TypeError: this. getOptions is not a function