【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 2Given tree t:
4
/ \
1 2Return 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
/
0Given tree t:
4
/ \
1 2Return false.
解题思路:我的方法很简单,用先序遍历的方法分别遍历这两棵树,并记录起遍历的路径,最后判断t的遍历路径是否是s的遍历路径的子串即可。
代码如下:
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution(object):
path = ''
def traverse(self,node,direction):
if node == None:
return
self.path += (direction + 'V' + str(node.val)) #节点值加上V前缀
if node.left != None:
self.traverse(node.left,'L') #左右节点分别加上标识
else:
self.path += 'LN'
if node.right != None:
self.traverse(node.right,'R')
else:
self.path += 'RN'
def isSubtree(self, s, t):
"""
:type s: TreeNode
:type t: TreeNode
:rtype: bool
"""
self.path = ''
self.traverse(s,'')
self.path += '#'
self.traverse(t,'')
pl = self.path.split('#')
#print self.path
return pl[0].find(pl[1]) != -1
【leetcode】572. Subtree of Another Tree的更多相关文章
- 【easy】572. Subtree of Another Tree
判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等) 有两种方法: 方法二:递归写法 //方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树 //方法二 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- 【LeetCode】572. 另一个树的子树 Subtree of Another Tree(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 方法二:DFS + DFS 方法三 ...
- 【LeetCode】 99. Recover Binary Search Tree [Hard] [Morris Traversal] [Tree]
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...
- 【LeetCode】98. Validate Binary Search Tree
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- 【LeetCode】105 & 106. Construct Binary Tree from Inorder and Postorder Traversal
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...
- 【LeetCode】98. Validate Binary Search Tree (2 solutions)
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- 问题记录-java图片验证码显示乱码
部署机器 操作系统:centos 7 java版本: java version "1.7.0_80" 问题症状 将一个java web的程序部署到了两台配置相同的服务器上之后(服务 ...
- [CSS]CSS浮动塌陷及解决办法
一. CSS浮动 先看一个例子 <html !DOCTYPE> <head> <title>HTML2</title> <style> .d ...
- centos7 安装gdal2.3.1
在直接源码安装gdal2.3时报错,大概意思是说没有安装SFCGAL. 1.centos更新cmake到3.5版本: wget https://cmake.org/files/v3.5/cmake-3 ...
- Window 相关命令
net user Administrator /Active:Yes NET USER 用于创建和修改计算机上的用户帐户.当不带选项使用本命令时,它会列出计算机上的用户帐户.用户帐户的信息存储在用户帐 ...
- Python笔记(十六)_else语句、with语句
else的多种用法 1.try except + else:检测到代码无异常,才执行else 例如: def func(num): count=num//2 while count>1: if ...
- Play with Chain 【HDU - 3487】【Splay+TLE讲解】
题目链接 很好的一道题,用了三天多的时间,终于知道了我为什么T的原因,也知道了在Splay的同时该怎样子的节约时间,因为Splay本身就是大常数的O(N*logN),我们如果不在各种细节上节约时间,很 ...
- SQLMap使用总结
支持模式:布尔/时间/报错/联合查询/堆查询 支持数据库:MySQL, Oracle, PostgreSQL, Microsoft SQL Server, Microsoft Access, IBM ...
- php不支持多线程怎么办
PHP 默认并不支持多线程,要使用多线程需要安装 pthread 扩展,而要安装 pthread 扩展,必须使用 --enable-maintainer-zts 参数重新编译 PHP,这个参数是指定编 ...
- 标准标签库JSTL(JSP Standard Tag Library)
1, 核心标签(最常用, 最重要的) 表达式控制标签 out 输出常量 value---直接赋值 输出变量 default---默认值 escapeXml---控制转义字符(默认为true, 如果需要 ...
- [Bzoj3224][Tyvj1728] 普通平衡树(splay/无旋Treap)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3224 平衡树入门题,学习学习. splay(学习yyb巨佬) #include<b ...