当前位置:网站首页>[leetcode refers to the substructure of offer 26. Tree (medium)]
[leetcode refers to the substructure of offer 26. Tree (medium)]
2022-04-23 21:20:00 【Minaldo7】
subject :
Input two binary trees A and B, Judge B Is it right? A Substructure of .( A convention empty tree is not a substructure of any tree )
B yes A Substructure of , namely A There are emergence and B Same structure and node values .
for example :
Given tree A:
Given tree B:
return true, because B And A A subtree of has the same structure and node values .

source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The problem solving process :
My method ( recursive )
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
public static boolean equals(TreeNode A,TreeNode B){
if(A==null&&B==null)
return true;
if(A==null)return false;
if(B==null)return true;
return A.val==B.val && equals(A.left, B.left) && equals(A.right, B.right);
}
public boolean isSubStructure(TreeNode A, TreeNode B) {
if(A==null || B == null)return false;
if(equals(A, B))return true;
return isSubStructure(A.left, B) || isSubStructure(A.right, B);
}
}
Execution results :

版权声明
本文为[Minaldo7]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/111/202204210544479693.html
边栏推荐
猜你喜欢

Identifier CV is not defined in opencv4_ CAP_ PROP_ FPS; CV_ CAP_ PROP_ FRAME_ COUNT; CV_ CAP_ PROP_ POS_ Frames problem

Keywords static, extern + global and local variables

The more you use the computer, the slower it will be? Recovery method of file accidental deletion

Flomo software recommendation

MySQL数据库常识之储存引擎

opencv应用——以图拼图

Question brushing plan -- backtracking method (I)

Fastdfs mind map

Addition, deletion, modification and query of advanced MySQL data (DML)

电脑越用越慢怎么办?文件误删除恢复方法
随机推荐
Some thoughts on super in pytorch, combined with code
C, print the source program of beautiful bell triangle
Sharpness difference (SD) calculation method of image reconstruction and generation domain index
Lunch on the 23rd day at home
MySQL进阶之常用函数
Deep analysis of C language function
On the three paradigms of database design
软件测试要怎么学?自学还是培训看完这篇文章你就懂了
Go limit depth traversal of files in directory
100天拿下11K,转岗测试的超全学习指南
阿里云回应用户注册信息泄露事件
South Korea may ban apple and Google from offering commission to developers, the first in the world
Alibaba cloud responded to the disclosure of user registration information
Matlab matrix index problem
go array
Explore ASP Net core read request The correct way of body
Google 尝试在 Chrome 中使用 Rust
UKFslam
ros功能包内自定义消息引用失败
Tensorflow realizes gradient accumulation, and then returns