博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
树的子结构
阅读量:3942 次
发布时间:2019-05-24

本文共 431 字,大约阅读时间需要 1 分钟。

在这里插入图片描述

递归啊递归,一看就会,一写就废。
思路:

1.遍历A树2.看以A树的每一个节点为根节点的树是不是包含B

在这里插入图片描述

class Solution {
public boolean isSubStructure(TreeNode A, TreeNode B) {
return (A!=null&&B!=null)&&(rec(A,B)||isSubStructure(A.left,B)||isSubStructure(A.right,B)); } public boolean rec(TreeNode A,TreeNode B){
if(B==null) return true; if(A==null||A.val!=B.val) return false; return rec(A.right,B.right)&&rec(A.left,B.left); }}

转载地址:http://vsnwi.baihongyu.com/

你可能感兴趣的文章
3.9.1 - Lists in Python
查看>>
3.9.2 - Lists - Adding and Removing Objects
查看>>
3.9.3 - Sorting Lists
查看>>
3.10 - Maya Commands: ls
查看>>
3.11 - Dictionaries in Python
查看>>
3.12 - Tuples in Python
查看>>
4.4 - For Loops
查看>>
4.2.2 - Logical and/or Operators
查看>>
Lesson 4 Part 2 Softmax Regression
查看>>
文章中运用到的数学公式
查看>>
Projective Dynamics: Fusing Constraint Projections for Fast Simulation
查看>>
从2D恢复出3D的数据
查看>>
glm 中 数据类型 与 原始数据(c++ 数组)之间的转换
查看>>
Derivatives of scalars, vector functions and matrices
查看>>
the jacobian matrix and the gradient matrix
查看>>
VS2010 将背景设为保护色
查看>>
ubutun里面用命令行安装软件
查看>>
ubuntu 常用命令
查看>>
SQLite Tutorial 4 : How to export SQLite file into CSV or Excel file
查看>>
Optimizate objective function in matrix
查看>>