[LeetCode]Subtree of Another Tree判断一棵树是不是另一棵树的子树
将树序列化为字符串,空节点用符号表示,这样可以唯一的表示一棵树。
用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判断一棵树是不是另一棵树的子树的更多相关文章
- [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 ...
- Python3解leetcode Subtree of Another Tree
问题描述: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...
- 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 ...
- 【LeetCode】572. 另一个树的子树 Subtree of Another Tree(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 方法二:DFS + DFS 方法三 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40
572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 ...
- LeetCode算法题-Subtree of Another Tree(Java实现)
这是悦乐书的第265次更新,第278篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第132题(顺位题号是572).给定两个非空的二进制树s和t,检查树t是否具有完全相同的 ...
- LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)
思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...
- [LeetCode]662. Maximum Width of Binary Tree判断树的宽度
public int widthOfBinaryTree(TreeNode root) { /* 层序遍历+记录完全二叉树的坐标,左孩子2*i,右孩子2*i+1 而且要有两个变量,一个记录本层节点数, ...
随机推荐
- Java面试专题-基础篇(1)
- 最新小样本学习综述 A Survey on Few-Shot Learning | 四大模型Multitask Learning、Embedding Learning、External Memory…
目录 原文链接: 小样本学习与智能前沿 01 Multitask Learning 01.1 Parameter Sharing 01.2 Parameter Tying. 02 Embedding ...
- python3 通过 pybind11 使用Eigen加速代码
python是很容易上手的编程语言,但是有些时候使用python编写的程序并不能保证其运行速度(例如:while 和 for),这个时候我们就需要借助c++等为我们的代码提速.下面是我使用pybind ...
- 老猿学5G:3GPP 5G规范中的URI资源概念
☞ ░ 前往老猿Python博文目录 ░ 说明: 本文参考3GPP29.501<Principles and Guidelines for Services Definition>结合笔者 ...
- PyQt(Python+Qt)学习随笔:QListView的isWrapping属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListView的isWrapping属性用于控制视图中的数据项项布局在可见区域中没有足够空间时是 ...
- PyQt(Python+Qt)学习随笔:toolButton的autoRaise和arrowType属性
autoRaise属性 autoRaise属性表示toolButton按钮是否自动凸出,类型为布尔类型.默认值为False,可以通过autoRaise().setAutoRaise(bool enab ...
- MySQL和sparkSQL合并行
表A 表B 从表A到表B MySQL 写法:select name, group_concat(score seperate ';') as score from A group by name sp ...
- webstorm2017.02版本如何使用material theme
本想废话一番,表达对material theme的喜欢.还是直接说方法吧~ file-settings-Plugins-Browse repositories-搜索 material theme -选 ...
- 使用pip安装pymysql出错;Could not find a version that satisfies the requirement cryptography (from pymysql) (from versions: ) No matching distribution found for cryptography (from pymysql)
今天使用pip安装pymysql时出现如下错误: Could not find a version that satisfies the requirement cryptography (from ...
- STL——容器(Map & multimap)的排序与遍历
1. Map & multimap 的排序与遍历 map<T1,T2,less<T1> > mapA; //该容器是按键的升序方式排列元素.如果未指定less< ...