当前位置:网站首页>[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
边栏推荐
猜你喜欢
Deno 1.13.2 发布
Some grounded words
MySQL基础之写表(创建表)
Common problems in deploying projects with laravel and composer for PHP
go interface
C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序
Arm architecture assembly instructions, registers and some problems
Problem brushing plan -- dynamic programming (IV)
Is rust more suitable for less experienced programmers?
Google tries to use rust in Chrome
随机推荐
On the three paradigms of database design
Explore ASP Net core read request The correct way of body
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
C, print the source program of beautiful bell triangle
Deno 1.13.2 发布
软件测试要怎么学?自学还是培训看完这篇文章你就懂了
GSI-ECM工程建设管理数字化平台
What about laptop Caton? Teach you to reinstall the system with one click to "revive" the computer
Amazon and epic will be settled, and the Microsoft application mall will be opened to third parties
Another data analysis artifact: Polaris is really powerful
100天拿下11K,转岗测试的超全学习指南
Problem brushing plan -- dynamic programming (III)
flomo软件推荐
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
Automatic heap dump using MBean
使用mbean 自动执行heap dump
Common problems in deploying projects with laravel and composer for PHP
Cmake project under vs2019: calculating binocular parallax using elas method
学会打字后的思考