将树序列化为字符串,空节点用符号表示,这样可以唯一的表示一棵树。

用list记录所有子树的序列化,和目标树比较。

List<String> list = new ArrayList<>();
public boolean isSubtree(TreeNode s, TreeNode t) {
helper(s);
helper(t);
String tt = list.get(list.size()-1);
for (int i = 0;i<list.size()-1;i++) {
if (list.get(i).equals(tt)) return true;
}
return false;
}
public String helper(TreeNode root)
{
StringBuilder cur = new StringBuilder();
if (root==null)
{
cur.append("#");
return new String(cur);
}
cur.append(root.val);
cur.append(helper(root.left));
cur.append(helper(root.right));
String s = new String(cur);
list.add(s);
return s;
}

LeetCode上还有更好地答案,是递归地判断每个节点的值是不是相等,也很好理解。

上边这种做法是一个大类的做法,就是每个节点都递归地构建一个变量,一般子树问题会经常用到

[LeetCode]Subtree of Another Tree判断一棵树是不是另一棵树的子树的更多相关文章

  1. [LeetCode] Subtree of Another Tree 另一个树的子树

    Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...

  2. Python3解leetcode Subtree of Another Tree

    问题描述: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...

  3. LeetCode - Subtree of Another Tree

    Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...

  4. 【LeetCode】572. 另一个树的子树 Subtree of Another Tree(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 方法二:DFS + DFS 方法三 ...

  5. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  6. LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40

    572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 ...

  7. LeetCode算法题-Subtree of Another Tree(Java实现)

    这是悦乐书的第265次更新,第278篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第132题(顺位题号是572).给定两个非空的二进制树s和t,检查树t是否具有完全相同的 ...

  8. LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)

      思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...

  9. [LeetCode]662. Maximum Width of Binary Tree判断树的宽度

    public int widthOfBinaryTree(TreeNode root) { /* 层序遍历+记录完全二叉树的坐标,左孩子2*i,右孩子2*i+1 而且要有两个变量,一个记录本层节点数, ...

随机推荐

  1. CentOS 7下使用systemctl为Nginx启用进程守护实现开机自启

    1.cd到指定目录 cd /usr/lib/systemd/system 2.创建nginx.service vi nginx.service 3.输入以下内容,路径为nginx安装路径 [Unit] ...

  2. Codeforces Round #688(Div 2) D. Checkpoints

    思路 第一步,先推导1,0,0,--,0,就是1后面跟了n-1个0的时候 所需要的期望步数 封闭式推导 \(f_n\)代表从n关开始直接通关需要的步数的期望 n为1的情况,即就只有一个1 \(f_1= ...

  3. PyQt(Python+Qt)学习随笔:布局控件layout的layoutSizeConstraint属性不起作用的问题解决办法

    在<PyQt(Python+Qt)学习随笔:布局控件layout的layoutSizeConstraint属性>中介绍layout的layoutSizeConstraint属性后,反复测试 ...

  4. Django 框架基本操作(二)

    一.设计表结构 1.班级表结构 表名:grade 字段:班级名称(gname).成立时间(gdate).女生总数(ggirlnum).男生总数(gboynum).是否删除(isDelete) 2.学生 ...

  5. LeetCode初级算法之数组:26 删除排序数组中的重复项

    删除排序数组中的重复项 题目地址:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/ 给定一个排序数组,你需要在 ...

  6. C# 继承类的值赋

    C# 继承类的值赋 /// <summary> /// 将父类的值赋值到子类中 /// </summary> /// <typeparam name="TPar ...

  7. Struts2 S2-061(CVE-2020-17530)漏洞复现

    0x00 漏洞简介 Apache Struts2框架是一个用于开发Java EE网络应用程序的Web框架.Apache Struts于2020年12月08日披露 S2-061 Struts 远程代码执 ...

  8. 微信小程序图片保存到本地

    微信小程序图片保存到本地是一个常用功能: 这里讲解下完整实现思路: 因为微信官方的授权只弹一次,用户拒绝后再次调用,就需要结合button组件的微信开放能力来调起,以下方案在微信各种授权中可参考. w ...

  9. STL——容器(Set & multiset)的查找

    1. set.find(elem); //查找elem元素,返回指向elem元素的迭代器. 1 #include <iostream> 2 #include <set> 3 4 ...

  10. 除了MD5加密,试一下签名吧

    需求 MD5加密,简单密码用彩虹表很容易破解. 密码不能被暴力破解,可以根据定制字符,时间戳等防止破解 解决方案 签名 密码加密 源码 const crypto = require('crypto') ...