当前位置:网站首页>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;
}
}
边栏推荐
- 鏈錶之雙指針(快慢指針,先後指針,首尾指針)
- Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
- Three.JS VR看房
- Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
- Expectation, variance and covariance
- 记录几个常见问题(202207)
- Common model making instructions
- leecode-学习笔记
- 2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
- Getting started stm32--gpio (running lantern) (nanny level)
猜你喜欢
谷歌地图案例
fibonacci search
Yiwen gets rid of the garbage collector
Element operation and element waiting in Web Automation
Usage Summary of scriptable object in unity
openresty ngx_ Lua request response
Spectrum analysis of ADC sampling sequence based on stm32
Error when LabVIEW opens Ni instance finder
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
Un article traite de la microstructure et des instructions de la classe
随机推荐
Distributed solution selection
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
Nangou Gili hard Kai font TTF Download with installation tutorial
Why does the C# compiler allow an explicit cast between IEnumerable&lt; T&gt; and TAlmostAnything?
First, redis summarizes the installation types
Fix the memory structure of JVM in one article
Vcomp110.dll download -vcomp110 What if DLL is lost
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
Starting from 1.5, build a micro Service Framework -- log tracking traceid
The difference between MVVM and MVC
记录几个常见问题(202207)
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
VOT Toolkit环境配置与使用
Three.js-01 入门
Overview of Fourier analysis
leecode-学习笔记
Matlab smooth curve connection scatter diagram
利用LNMP实现wordpress站点搭建
一文搞定JVM常见工具和优化策略
openresty ngx_lua正则表达式