当前位置:网站首页>Leetcode 101 symmetric binary tree
Leetcode 101 symmetric binary tree
2022-07-21 07:28:00 【showbuger】
recursive
var isSymmetric = function(root) {
return dfs(root, root);
};
// The condition of symmetry is that the left subtree of a tree is mirrored symmetrically with its right subtree
// Both pointers start from the root node at the same time
// One to the left , One to the right
// If the two values are different , Then it means it is not symmetrical
const dfs = (p, q) => {
if (p === null && q === null) return true;
if (p === null || q === null) return false;
return p.val === q.val && dfs(p.left, q.right) && dfs(p.right, q.left);
}
Guang Shu
var isSymmetric = function(root) {
let queue = [root, root];
while (queue.length > 0) {
let l = queue.shift();
let r = queue.shift();
if (l === null && r === null) {
continue;
}
if (l === null || r === null || l.val !== r.val) {
return false;
}
// Each time you join the team, first join the left node of the first pointer , And the right node of the second pointer
// Make sure to compare the left node with the right node when taking out the first two of the team , This is mirror symmetric
queue.push(l.left);
queue.push(r.right);
// Then queue the right node of the first pointer , And the left node of the second pointer
queue.push(l.right);
queue.push(r.left);
}
return true;
};
边栏推荐
猜你喜欢
[computer gadget recommendation] sedentary reminder
【Flutter -- 顶部导航栏】TabBarView 的基本使用
[shutter -- basic component] image & Icon
14 mental models in the encryption Market
Peripherals: ADC
MySQL索引的数据结构
什么?多商户系统不适配 APP?这不就来了么!
EasyGBS切换分辨率出现花屏和播放失败的原因分析及解决方法
外设篇:LCD显示器
Tiflash source code reading (V) deltatree storage engine design and implementation analysis - Part 2
随机推荐
KusionStack 开源|Kusion 模型库和工具链的探索实践
QT之隐藏控件
Network and VPC experiment
MySQL索引的数据结构
基因组科学数据的安全管理与应用
mongodb安全集群搭建
生态系统长期观测数据产品体系
IO advanced advanced IO
[shutter -- basic component] use of AppBar
web页面的点对点复制粘贴
[technical dry goods] driver development and abnormal analysis of industrial touch screen (serial)
what? Does the multi merchant system not adapt to app? This is coming!
外设篇:NandFlash和iNand
C陷阱和缺陷 第1章 词法“陷阱” 1.1 =不同于==
[英雄星球七月集训LeetCode解题日报] 第20日 BST
Moco V2: further upgrade of Moco series
动手搭建一个三个节点的eureka集群
C陷阱与缺陷 第2章 语法“陷阱” 2.1 理解函数声明
[shutter -- basic component] image & Icon
Peripherals: I2C communication details