当前位置:网站首页>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;
}
}
边栏推荐
- 傅里叶分析概述
- MCU case -int0 and INT1 interrupt count
- [untitled]
- Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
- Composition of interface
- leecode-学习笔记
- Event trigger requirements of the function called by the event trigger
- Codeforces Global Round 19
- 数据库基础知识(面试)
- [speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
猜你喜欢

Simple and beautiful method of PPT color matching

audiopolicy

Vision Transformer (ViT)

Ieventsystemhandler event interface

CJ mccullem autograph: to dear Portland

First, redis summarizes the installation types

Starting from 1.5, build a micro Service Framework -- log tracking traceid

Common model making instructions

Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
![[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]](/img/03/8fa104b177698a15b7ffa70d4fb524.jpg)
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
随机推荐
Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)
Why does the C# compiler allow an explicit cast between IEnumerable&lt; T&gt; and TAlmostAnything?
【Note17】PECI(Platform Environment Control Interface)
Alibaba Tianchi SQL training camp task4 learning notes
Yiwen gets rid of the garbage collector
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
媒体查询:引入资源
二叉树(二)——堆的代码实现
d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
Ieventsystemhandler event interface
【无标题】
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
Openresty ngx Lua regular expression
记录几个常见问题(202207)
Three. Js-01 getting started
MoCo: Momentum Contrast for Unsupervised Visual Representation Learning