当前位置:网站首页>LeetCode#572 另一颗树的子树
LeetCode#572 另一颗树的子树
2022-07-20 11:40:00 【showbuger】
递归
var isSubtree = function(root, subRoot) {
return dfs(root, subRoot);
};
// 本次递归是找根节点
const dfs = (s, t) => {
if (s === null) {
return false;
}
// 检查当前节点的树 或者 左子树 或 右子树
return check(s, t) || dfs(s.left, t) || dfs(s.right, t);
}
// 本次递归是检查树的每个节点
const check = (s, t) => {
if (s === null && t === null) {
return true;
}
if (s === null || t === null || s.val !== t.val) {
return false;
}
// 同时递归检查左子树和右子树
return check(s.left, t.left) && check(s.right, t.right);
}
难的解法以后再学
边栏推荐
猜你喜欢
what? Does the multi merchant system not adapt to app? This is coming!
C traps and defects Chapter 2 syntax "traps" 2.5 function calls
CV (3)- Loss Functions and Optimization
Moco V2: further upgrade of Moco series
外设篇:LCD显示器
Network and VPC experiment
Alibaba cloud load balancing experiment July 11, 2022
Eolink ——通过文档驱动,快速开发接口
Peripherals: touch screen
Use ECs and OSS to set up personal network disk
随机推荐
Global location number Gln application introduction
ARM64 内存64位宽与32位位宽 速度对比
RKE vs. RKE2:对比两种 Kubernetes 发行版
C traps and defects Chapter 2 syntax "traps" 2.1 understanding function declarations
lambda
多线程与高并发(二)—— Synchronized 加锁解锁流程
C and pointer Chapter 2 syntax "trap" 2.2 priority of operators
开源的远程桌面软件RustDesk
C陷阱和缺陷 第1章 词法“陷阱” 1.1 =不同于==
How to download video files
抽丝剥茧C语言(高阶)文件操作+练习
IO进阶之高级IO
用Reader模拟BufferedReader的readLine()功能
【C语言】计算器实现
C traps and defects Chapter 2 lexical "traps" 2.3 pay attention to the semicolon marked as the end of the sentence
NAT 的几种应用场景
thymeleaf在网页中上传文件
G024-DB-GS-INS-01 OpenEuler 部署 OpenGauss(单实例)
数据库表设计规范汇总
使用 CSP进行欺诈检测