二叉树子树

leetcode572
问题:判断t是否为s的子树

1
2
3
4
5
6
7
8
9
10
11
python解决方案
class TreeNode:
def __init__(self):
self.val = val
self.left = None
self.right = None
class Solution:
def isSubtitle(t,s):
def up(t):
return (t.val, up(t.left), up(t.right)) if t else None
return str(up(t)) in str(up(s))

注意点:

  • 元组中的元素位置不会发生变化
  • print(“a” in “ab”) 返回true