当前位置:网站首页>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;
}
}
边栏推荐
- Matlab smooth curve connection scatter diagram
- Event trigger requirements of the function called by the event trigger
- Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
- audiopolicy
- Registration and skills of hoisting machinery command examination in 2022
- 2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
- Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
- 数据库基础知识(面试)
- openresty ngx_lua请求响应
- 【无标题】
猜你喜欢

Three.JS VR看房

How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?

TypeError: this. getOptions is not a function

Google Maps case

一文搞定垃圾回收器

VOT toolkit environment configuration and use

Negative sampling

Finally understand what dynamic planning is

Expectation, variance and covariance

【Note17】PECI(Platform Environment Control Interface)
随机推荐
Thinkphp5.1 cross domain problem solving
Three. Js-01 getting started
Element operation and element waiting in Web Automation
Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share
fibonacci search
PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
基于STM32的ADC采样序列频谱分析
Exponential weighted average and its deviation elimination
第十七周作业
南京:全面启用商品房买卖电子合同
Three.js-01 入门
Composition of interface
Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
Binary tree (II) -- code implementation of heap
Usage Summary of scriptable object in unity
2.13 summary
Nacos installation and service registration
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Event trigger requirements of the function called by the event trigger
分布式解决方案之TCC