当前位置:网站首页>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;
}
}
边栏推荐
- TypeError: this. getOptions is not a function
- Selenium+pytest automated test framework practice
- Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
- 从 1.5 开始搭建一个微服务框架——日志追踪 traceId
- Three. JS VR house viewing
- 从 1.5 开始搭建一个微服务框架——日志追踪 traceId
- Un article traite de la microstructure et des instructions de la classe
- The code generator has deoptimised the styling of xx/typescript.js as it exceeds the max of 500kb
- Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
- Openresty ngx Lua regular expression
猜你喜欢
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
Un article traite de la microstructure et des instructions de la classe
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
Finally understand what dynamic planning is
fibonacci search
LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
[untitled]
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework
TypeError: this. getOptions is not a function
随机推荐
I closed the open source project alinesno cloud service
Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
TCC of distributed solutions
傅里叶分析概述
Lesson 1: serpentine matrix
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
派对的最大快乐值
分布式解决方案选型
Douban scoring applet Part-2
Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share
audiopolicy
Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
【无标题】
链表之双指针(快慢指针,先后指针,首尾指针)
Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
Three. Js-01 getting started
东南亚电商指南,卖家如何布局东南亚市场?
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
Editor extensions in unity
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