当前位置:网站首页>Sword finger offer 26: substructure of tree
Sword finger offer 26: substructure of tree
2022-06-22 02:06:00 【Swarford】
subject :
Ideas :
Set recursion in recursion ;
A subtree is not necessarily A root node root1 Start , Traverse the preamble A Each node of --------- recursive 1
When traversing A The current node of root1 As a starting point , Judge B Whether it's the current root1 The subtree of --------- recursive 2
Every time I traverse from A when root1 set out , Call recursion 2 To judge B Is it root1 The subtree of , if root1 and B The root nodes of are not equal , Then there is no need to recurse , direct false, That is, the root nodes of the current two trees are different ;
When root1 and root2 Isochronous , Just started to recurse downward
Java Realization :
import java.util.*;
public class Solution {
public boolean HasSubtree(TreeNode root1,TreeNode root2) {
if(root1==null || root2==null){
return false; // Filter first A、B There is null All back to false
}
// The former sequence traversal
// Traverse A, At present root1 As a starting point , To judge B Is it root1 The subtree of ,
boolean a=check(root1,root2);
// Recursive traversal A Child nodes of
boolean left=HasSubtree(root1.left,root2);
boolean right=HasSubtree(root1.right,root2);
return a || left || right; // Either case true be true
}
// Judge B Is it A subtree
Boolean check(TreeNode root1,TreeNode root2){
if(root1==null && root2==null){
// Termination conditions , incoming A B It can't all be null, The front has been filtered out
return true;
}
if(root1!=null && root2==null){
//A It's not over yet ,B It's gone , explain B yes A Part of , be true !!!
return true;
}
if(root1==null && root2!=null){
//A It's all done ,B also , explain B There are redundant ,
return false;
}
// Traversing the tree Before the order
if(root1.val!=root2.val){
return false;
}
Boolean left=check(root1.left,root2.left);
Boolean right=check(root1.right,root2.right);
return left && right;
}
}
边栏推荐
- Learn to crawl steadily 08 - detailed explanation of the use method of selenium
- Chapter 09 English printed character recognition based on feature matching matlab deep learning practical case
- 如何获取自由和财富
- 数字信号处理
- idea----复制黏贴
- 中午不睡觉下午困
- acwing 838. Heap sort (write a heap)
- Recommended by Ali, Tencent and Baidu software testing engineers - waterfall model of software testing model
- MBA-day24 最值问题
- Games-101-personal summary shading
猜你喜欢

excel常用快捷键excel快捷键汇总

Games-101 personal summary rasterization
![[phantom engine UE] package error appears! Solutions to findpin errors](/img/d5/6747e20da6a8a4ca461094bd27bbf0.png)
[phantom engine UE] package error appears! Solutions to findpin errors

Commission contract on BSV

excel常用快捷鍵excel快捷鍵匯總

Chapter 18 build a general video processing tool based on GUI matlab application GUI implementation

Games-101-personal summary shading

微信小程序影视评论交流平台系统毕业设计毕设(7)中期检查报告

acwing 838. Heap sort (write a heap)

Appium interview questions
随机推荐
Chapter 18 build a general video processing tool based on GUI matlab application GUI implementation
What is your understanding of interface testing?
测试apk-异常管控Sensor攻击者开发
acwing 837. Number of points in connected blocks (additional information maintained by querying sets - number of sets)
Commission contract on BSV
Test APK exception control sensor attacker development
微信小程序影视评论交流平台系统毕业设计毕设(7)中期检查报告
How to gain freedom and wealth
[chapter 01 image defogging technology based on histogram optimization - full system matlab intelligent driving depth learning]
IE浏览器自动跳转edge怎么恢复
Leetcode 41 - 45 dynamic planning topic
Download links to components, frameworks and development tools commonly used by programmers
五笔 第一讲 指法
excel常用快捷键excel快捷键汇总
Machine learning compilation lesson 1: overview of machine learning compilation
[chapter 04 answer sheet recognition based on Hough change]
Test case design method -- cause and effect diagram method
Chrome浏览器取消输入框记录表单输入历史
LeetCode 41 - 45 动态规划专题
Commission contract on BSV (2)