当前位置:网站首页>[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
边栏推荐
- go map
- Is qiniu school useful and is the recommended securities account safe
- Tensorflow1. X and 2 How does x read those parameters saved in CKPT
- C#,打印漂亮的贝尔三角形(Bell Triangle)的源程序
- pikachuxss如何获取cookie靶场,返回首页总是失败
- Mysql database common sense storage engine
- Google 尝试在 Chrome 中使用 Rust
- The iswow64process function determines the number of program bits
- 笔记本电脑卡顿怎么办?教你一键重装系统让电脑“复活”
- Is rust more suitable for less experienced programmers?
猜你喜欢
Fastdfs思维导图
Mysql database common sense storage engine
Reentrant function
Prim、Kruskal
go defer
Arm architecture assembly instructions, registers and some problems
Xiaomi mobile phone has abandoned the "Mi" brand all over the world and switched to the full name brand of "Xiaomi"
[matlab 2016 use mex command to find editor visual studio 2019]
ROS学习笔记-----ROS的使用教程
随机推荐
Tensorflow realizes gradient accumulation, and then returns
Sklearn function: Train_ Test_ Split (split training set and test set)
Minecraft 1.12.2模组开发(四十三) 自定义盾牌(Shield)
matplotlib. Pyplot partition drawing
go map
Explore ASP Net core read request The correct way of body
2.整理华子面经--2
软件测试要怎么学?自学还是培训看完这篇文章你就懂了
启牛学堂有用吗,推荐的证券账户是否安全
Deno 1.13.2 发布
CUDA, NVIDIA driver, cudnn download address and version correspondence
危机即机遇,远程办公效率为何会提升?
MySQL进阶之常用函数
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
ubutnu20安裝CenterNet
引入结构化并发,Swift 5.5 发布!
Another data analysis artifact: Polaris is really powerful
Problem brushing plan -- dynamic programming (III)
Zhongchuang storage | how to choose a useful distributed storage cloud disk
1.整理华子面经--1