当前位置:网站首页>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;
}
}
边栏推荐
- Thinkphp5.1 cross domain problem solving
- 一文搞定class的微观结构和指令
- d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
- 从 1.5 开始搭建一个微服务框架——日志追踪 traceId
- Yiwen gets rid of the garbage collector
- Solve the problem of "no input file specified" when ThinkPHP starts
- Arduino 测量交流电流
- Binary tree (III) -- heap sort optimization, top k problem
- SPSS analysis of employment problems of college graduates
- Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
随机推荐
30 optimization skills about mysql, super practical
Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share
Media query: importing resources
Record several frequently asked questions (202207)
Negative sampling
抖音__ac_signature
Binary tree (III) -- heap sort optimization, top k problem
Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
audiopolicy
Tensor attribute statistics
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
SPSS analysis of employment problems of college graduates
东南亚电商指南,卖家如何布局东南亚市场?
Three. Js-01 getting started
Solve the problem of "no input file specified" when ThinkPHP starts
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
Activate function and its gradient
MCU case -int0 and INT1 interrupt count
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
分布式解决方案之TCC