剑指 Offer 26. 树的子结构
2023-02-18 16:35:25 时间
题目:
思路:
【1】对于树的判断其实没有什么比较好的方式,一般都是遍历,判断是不是子树,那么就是比较内容相不相等。将主树拿出来进行遍历,同时又可以拆分为左右两个子树,形成的子树又可以作为新的主树与比较的树再次进行比较,本身就是利用了 深度优先搜索 的概念。
代码展示:
//树的子结构 public class Offer { public boolean isSubStructure(TreeNode A, TreeNode B) { // A, B为空,则不存在子树 if(A == null || B == null) { return false; } // dfs(A, B) 当前节点B是否是A的子树,若不是,则同理判断当前节点的孩子节点 return dfs(A, B) || isSubStructure(A.left, B) || isSubStructure(A.right, B); } public boolean dfs(TreeNode A, TreeNode B){ // 比较孩子节点时, B可以为空, 例如[1]是[5,1]的子树 if(B == null) { return true; } // A为空, B不为空 B一定不是A子树 if(A == null) { return false; } // 若两个节点的值不同 则B不可能是A的子树 相同则比较两个节点的孩子节点是否相同 return A.val == B.val && dfs(A.left, B.left) && dfs(A.right, B.right); } } class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } }
相关文章
- 谣言检测——(GLAN)《Jointly embedding the local and global relations of heterogeneous graph for rumor detection》
- 谣言检测——《MFAN: Multi-modal Feature-enhanced Attention Networks for Rumor Detection》
- 谣言检测——《社会网络谣言检测综述》
- 论文解读(PairNorm)《PairNorm: Tackling Oversmoothing in GNNs》
- 论文解读(NWR)《Graph Auto-Encoder via Neighborhood Wasserstein Reconstruction》
- 论文解读(GATv2)《How Attentive are Graph Attention Networks?》
- 论文解读(SEP)《Structural Entropy Guided Graph Hierarchical Pooling》
- 论文解读(ASAP)《ASAP: Adaptive Structure Aware Pooling for Learning Hierarchical Graph Representations》
- 论文解读(Graph Barlow Twins)《Graph Barlow Twins: A self-supervised representation learning framework for graphs》
- 论文解读(ChebyGIN)《Understanding Attention and Generalization in Graph Neural Networks》
- 论文解读(g-U-Nets)《Graph U-Nets》
- 论文解读(soft-mask GNN)《Soft-mask: Adaptive Substructure Extractions for Graph Neural Networks》
- 论文解读( AF-GCL)《Augmentation-Free Graph Contrastive Learning with Performance Guarantee》
- 论文解读(PPNP)《Predict then Propagate: Graph Neural Networks meet Personalized PageRank》
- 论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
- 论文解读《On the Prediction Instability of Graph Neural Networks》
- 论文解读(AGC)《Attributed Graph Clustering via Adaptive Graph Convolution》
- 论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》
- 论文解读(LGAE)《Simple and Effective Graph Autoencoders with One-Hop Linear Models》
- 期望、方差、协方差