当前位置:网站首页>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;
}
}
边栏推荐
- Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
- Alibaba Tianchi SQL training camp task4 learning notes
- Overview of Fourier analysis
- Starting from 1.5, build a micro Service Framework -- log tracking traceid
- LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
- openresty ngx_lua请求响应
- Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
- Roman numeral to integer
- 【Note17】PECI(Platform Environment Control Interface)
- 2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
猜你喜欢
查看网页最后修改时间方法以及原理简介
My experience and summary of the new Zhongtai model
d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
[untitled]
Activate function and its gradient
Starting from 1.5, build a micro Service Framework -- log tracking traceid
VOT Toolkit环境配置与使用
Unity Max and min constraint adjustment
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
Tensor attribute statistics
随机推荐
Three.js-01 入门
Exponential weighted average and its deviation elimination
Un article traite de la microstructure et des instructions de la classe
【Note17】PECI(Platform Environment Control Interface)
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
Activate function and its gradient
Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
南京:全面启用商品房买卖电子合同
Starting from 1.5, build a micro Service Framework -- log tracking traceid
Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
MCU case -int0 and INT1 interrupt count
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
二叉树(二)——堆的代码实现
LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Nacos 的安装与服务的注册
谷歌地图案例
Nacos installation and service registration
Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
一文搞定JVM常见工具和优化策略