LeetCode 572. 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.
题目标签:Tree
这道题目给了我们两个二叉树s, t,让我们判断一下,t 是不是 s 的子树。首先我们想一下,如果 s 和 t 一摸一样,那么 t 也是 s 的子树。如果s和t不一样, 那么我们要依次从 s 的 left 当作一个新的树,和 t 比较,如果不是;从 s 的 right 当作一个新的树,和 t 比较。直到把 s 树遍历结束;其中如果遇到 s 等于 t 的情况,立即返回true即可。那么我们要另外设一个function,来判断一下是否两个二叉树是相同的。
Java Solution:
Runtime beats 83.10%
完成日期:06/30/2017
关键词:Tree
关键点:recursively call trick:
recursively - return function(left) || function(right) : 只需要两个中任何一个function call 的返回值是true,那么答案就是true,运用在遍历 s 树的每一个点 和 t 比较;
recursively - return function(left) && function(right): 需要两个children都是返回true, 答案才是true,运用在判断两颗树否则相同。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
public boolean isSubtree(TreeNode s, TreeNode t)
{
if(s == null)
return false; if(isSame(s,t)) // check is tree s and t are same
return true; // if tree s and t are not same, recursively call to pass s children
return isSubtree(s.left, t) || isSubtree(s.right, t);
} public boolean isSame(TreeNode s, TreeNode t)
{
if(s == null && t == null) // if two nodes are null, they are same
return true; if(s == null || t == null) // if one node is null, another is not null, they are not same
return false; if(s.val != t.val) // if two node values are not same
return false; // if two node values are same, continue to check their children until end of tree
return isSame(s.left, t.left) && isSame(s.right, t.right);
}
}
参考资料:
http://www.cnblogs.com/grandyang/p/6828687.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 572. Subtree of Another Tree (是否是另一个树的子树)的更多相关文章
- LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40
572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 ...
- [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 ...
- 572. Subtree of Another Tree(easy)
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...
- [程序员代码面试指南]二叉树问题-判断t1树是否包含t2树的全部拓扑结构、[LeetCode]572. 另一个树的子树
题目1 解 先序遍历树1,判断树1以每个节点为根的子树是否包含树2的拓扑结构. 时间复杂度:O(M*N) 注意区分判断总体包含关系.和判断子树是否包含树2的函数. 代码 public class Ma ...
- LeetCode 572. 另一个树的子树 | Python
572. 另一个树的子树 题目来源:https://leetcode-cn.com/problems/subtree-of-another-tree 题目 给定两个非空二叉树 s 和 t,检验 s 中 ...
- Java实现 LeetCode 572 另一个树的子树(遍历树)
572. 另一个树的子树 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树 ...
- LeetCode 572. 另一个树的子树
题目链接:https://leetcode-cn.com/problems/subtree-of-another-tree/ 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和 ...
- 力扣Leetcode 572. 另一个树的子树
另一个树的子树 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 示例 ...
- 【LeetCode】572. 另一个树的子树 Subtree of Another Tree(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 方法二:DFS + DFS 方法三 ...
随机推荐
- SQL数据库基础知识-巩固篇<一>
SQL数据库基础知识-巩固篇<一>... =============== 首先展示两款我个人很喜欢的数据库-专用于平时个人SQL技术的练习<特点:体积小,好安装和好卸载,功能完全够用 ...
- json:JSONObject与JSONArray的使用
1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons ...
- python实例编写(1)--浏览器操作,元素操作
一.浏览器操作 1. back()与 forward() #coding=gbk //编码不一定是utf-8 from selenium import webdriver //导入包,也叫”模组“ ...
- 深入理解计算机系统(2.7)------二进制小数和IEEE浮点标准
整数的表示和运算我们已经讲完了,在实际应用中,整数能够解决我们大部分问题.但是某些需要精确表示的数,比如某件商品的价格,某两地之间的距离等等,我们如果用整数表示将会有很大的出入,这时候浮点数就产生了. ...
- mongoDB学习手记1--Windows系统下的安装与启动
第一步:下载安装包 我们首先需要下载 mongodb 的安装包,直接到官网下载即可.地址为:https://www.mongodb.com/download-center#community. 看下自 ...
- Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...
- 【JAVA零基础入门系列】Day3 Java基本数据类型
前两篇已经将开发环境搭建完成,如果你已经按之前的教程按部就班的完成了部署,那么世界上最优秀的编程语言之一和世界上最优秀的IDE之一已经出现在你的电脑上(此处应有掌声),如果你还没入门,或者正在台阶上踱 ...
- easyUI表单基础知识
easyUI创建异步提交表单 我们创建一个带有 name.email 和 phone 字段的表单.通过使用 easyui 表单(form)插件来改变表单(form)为 ajax 表单(form).表单 ...
- Canal 同步异常分析:Could not find first log file name in binary log index file
文章首发于[博客园-陈树义],点击跳转到原文Canal同步异常分析:Could not find first log file name in binary log index file. 公司搜索相 ...
- zoj 1526 Big Number 数学
Big Number Time Limit: 10 Seconds Memory Limit: 32768 KB In many applications very large intege ...