当前位置:网站首页>Leetcode 572 subtree of another tree
Leetcode 572 subtree of another tree
2022-07-21 07:28:00 【showbuger】
recursive
var isSubtree = function(root, subRoot) {
return dfs(root, subRoot);
};
// This recursion is to find the root node
const dfs = (s, t) => {
if (s === null) {
return false;
}
// Check the tree of the current node perhaps The left subtree or Right subtree
return check(s, t) || dfs(s.left, t) || dfs(s.right, t);
}
// This recursion is to check each node of the tree
const check = (s, t) => {
if (s === null && t === null) {
return true;
}
if (s === null || t === null || s.val !== t.val) {
return false;
}
// At the same time, recursively check the left subtree and the right subtree
return check(s.left, t.left) && check(s.right, t.right);
}
Learn the difficult solution later
边栏推荐
- [shutter -- layout] basic use of container
- #yyds干货盘点# 解决名企真题:罪犯转移
- C和指针 第1章 词法“陷阱” 1.3 词法分析中的“贪心法”
- Start to build a three node Eureka cluster
- TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
- 科学数据中心资源和用户访问控制体系
- Kubesphere 3.3.0 offline installation tutorial
- 接口自动化测试---pytest测试用例设计
- C和指针 第1章 词法“陷阱” 1.4 整型常量
- Delegation mechanism of simplified governance of Dao
猜你喜欢
CV (3)- Loss Functions and Optimization
C陷阱与缺陷 第2章 语法“陷阱” 2.5 函数调用
动态内存管理(malloc,free,calloc,realloc,柔性数组)
外设篇:NandFlash和iNand
[shutter -- basic component] text component text & TextStyle & textspan
多线程与高并发(二)—— Synchronized 加锁解锁流程
Use ECs and OSS to set up personal network disk
C和指针 第1章 词法“陷阱” 1.4 整型常量
【Ping检测】使用Ping命令检查网络连接情况
flinksql读取kafka数据报错
随机推荐
lambda
玩转Makefile文件
[C language] calculator implementation
flinksql读取kafka数据报错
用Reader模拟BufferedReader的readLine()功能
The ability to detect movement in vivo and build a safe and reliable payment level "face brushing" experience
LeetCode#572 另一颗树的子树
C陷阱与缺陷 第2章 语法“陷阱” 2.1 理解函数声明
Kubesphere 3.3.0 offline installation tutorial
Form Data与Request Payload,你真的了解吗?
C陷阱与缺陷 第2章 语法“陷阱” 2.5 函数调用
数据库表设计规范汇总
自定义类型:结构体,枚举,联合
#yyds干货盘点# 解决名企真题:罪犯转移
IO进阶之高级IO
移动机器人学(三)卡尔曼滤波
C和指针 第1章 词法“陷阱” 1.4 整型常量
外设篇:I2C通信详解
外设篇:LCD显示器
Network and VPC experiment