LeetCode - Subtree of Another Tree
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself. Example 1:
Given tree s: 3
/ \
4 5
/ \
1 2
Given tree t:
4
/ \
1 2
Return true, because t has the same structure and node values with a subtree of s.
Example 2:
Given tree s: 3
/ \
4 5
/ \
1 2
/
0
Given tree t:
4
/ \
1 2
Return false.
还是用到了递归和序列化的解法:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
String str1 = serialize(s);
String str2 = serialize(t);
return str1.contains(str2);
} private String serialize (TreeNode root){
if(root == null){return "#";}
String serial = "<"+root.val+">"+ serialize(root.left) + serialize (root.right);
return serial;
}
}
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判断一棵树是不是另一棵树的子树
将树序列化为字符串,空节点用符号表示,这样可以唯一的表示一棵树. 用list记录所有子树的序列化,和目标树比较. List<String> list = new ArrayList< ...
- LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40
572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- LeetCode: Validata Binary Search Tree
LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
随机推荐
- MySQL 数据库备份策略:全备与增量备份
一.备份策略1.周日全备份,周一至周六增量备份2.全备份目录/u03/backup/innobackup/full_backup3.增量备份目录/u03/backup/innobackup/incre ...
- C++定义自己的异常
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- AVL平衡二叉树实现,图解分析,C++描述,完整可执行代码
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- Java的第一个晞月自己打的程序
1.编写一个程序,求1!+2!+…+10!的值. package xxx; public class abc { public static void main(String args[]) { in ...
- 201621123001 《Java程序设计》第14周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. Mysql数据库简单操作,常用的操作命令 启动:进入Mysql (从命令行mysql -u root -p) 退 ...
- Linux文件系统命令 pwd
命令名:pwd 功能:查看当前所处的位置 eg: renjg@renjg-HP-Compaq-Pro--MT:~$ pwd /home/renjg renjg@renjg-HP-Compaq-Pro- ...
- <Google><APAC><kickstart><2017.05.07><2017RoundB>
Google APAC kickstart 网址链接 我的所有solution代码和文件请点击 前言 这个比赛的题怎一个变态了得,虽然是第一次参赛,抱着熟悉流程的心态去的,但仍然被虐得一颤一颤的╮(╯ ...
- UIIimageView读取图片的两种方式及动画的执行
/**count:图片数量 name:图片名称*/ - (void)runAnimationWithCount:(int)count name:(NSString *)name { if(self.t ...
- git使用简明教程
1.自己在gitlab.XXX.com创建一个项目 点击右上角的"+"符号,创建新项目. 项目名:xxxtest 2.在master分支提交一个文件Readme.txt 文件内容: ...
- 如何让你的 React Native 应用在键盘弹出时优雅地响应
原文地址:How to make your React Native app respond gracefully when the keyboard pops up 原文作者:Spencer Car ...