当前位置:网站首页>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;
}
}
边栏推荐
- Starting from 1.5, build a micro Service Framework -- log tracking traceid
- Nacos installation and service registration
- 一文搞定垃圾回收器
- Openresty ngx Lua regular expression
- My experience and summary of the new Zhongtai model
- [digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
- Finally understand what dynamic planning is
- 实现反向代理客户端IP透传
- The code generator has deoptimised the styling of xx/typescript.js as it exceeds the max of 500kb
- 傅里叶分析概述
猜你喜欢

2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank

一文搞定class的微观结构和指令

Ultrasonic sensor flash | LEGO eV3 Teaching

Arduino 测量交流电流
![[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]](/img/41/4de83d2c81b9e3485d503758e12108.jpg)
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]

Google Maps case

Fix the memory structure of JVM in one article

TypeError: this. getOptions is not a function

【无标题】

TCC of distributed solutions
随机推荐
Getting started stm32--gpio (running lantern) (nanny level)
[screen recording] how to record in the OBS area
一文搞定class的微觀結構和指令
Request preview display of binary data and Base64 format data
Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
Metaverse ape ape community was invited to attend the 2022 Guangdong Hong Kong Macao Great Bay metauniverse and Web3.0 theme summit to share the evolution of ape community civilization from technology
东南亚电商指南,卖家如何布局东南亚市场?
两数之和、三数之和(排序+双指针)
Global and Chinese markets for welding products 2022-2028: Research Report on technology, participants, trends, market size and share
二叉树(三)——堆排序优化、TOP K问题
My experience and summary of the new Zhongtai model
Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
Arduino 测量交流电流
Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
Global and Chinese market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
Nangou Gili hard Kai font TTF Download with installation tutorial
如何快速理解复杂业务,系统思考问题?
Distributed solution selection
Three. Js-01 getting started
Fix the memory structure of JVM in one article