LeetCode--572. Subtree of Another Tree(另一个树的子树)

1人阅读 评论(0) 收藏 举报
分类:

题目:
给定两个二叉树,s和t,判断t是否是s的一个子树。

解题思路:
先定义一个函数来判断s和t是否完全相同。再对s的每一个节点遍历,看s以该节点为根节点,是否和t完全相同。

代码(Python):

# 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):
    def isSubtree(self, s, t):
        """
        :type s: TreeNode
        :type t: TreeNode
        :rtype: bool
        """
        def IfSame(s,t):
            if s==None and t==None:
                return True
            elif s==None and t!=None:
                return False
            elif s!=None and t==None:
                return False
            if s.val==t.val:
                return IfSame(s.left,t.left) and IfSame(s.right,t.right)
        
        def Sub_s(s,t):
            if IfSame(s,t):
                return True
            elif s!=None:
                return Sub_s(s.left,t) or Sub_s(s.right,t)
            else:
                return False
            
        return Sub_s(s,t)

查看评论

[LeetCode]572. Subtree of Another Tree(判断树t是否是树S的子树)

572. Subtree of Another Tree原题链接 Given two non-empty binary trees s and t, check whether tree t has...
  • whl_program
  • whl_program
  • 2017-05-28 00:21:38
  • 335

【LeetCode】Subtree of Another Tree 解题报告

【LeetCode】Subtree of Another Tree 解题报告标签(空格分隔): LeetCode题目地址:https://leetcode.com/problems/subtree-o...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2017-05-09 11:54:47
  • 1050

[leetcode: Python]572. Subtree of Another Tree

Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no...
  • NXHYD
  • NXHYD
  • 2017-05-19 16:32:45
  • 1314

572. Subtree of Another Tree(Easy)

原题目:   Given two non-empty binary trees s and t, check whether tree t has exactly the same structur...
  • wyh476901857
  • wyh476901857
  • 2017-05-08 20:32:29
  • 773

LeetCode-572. Subtree of Another Tree (JAVA)子树

LeetCode-572. Subtree of Another Tree (JAVA)子树
  • mine_song
  • mine_song
  • 2017-06-08 17:42:14
  • 918

572. Subtree of Another Tree (另一个树的子树)

Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no...
  • NichChen
  • NichChen
  • 2017-10-13 11:14:03
  • 66

leetcode572. Subtree of Another Tree

572. Subtree of Another TreeGiven two non-empty binary trees s and t, check whether tree t has exact...
  • rxt2012kc
  • rxt2012kc
  • 2017-06-03 20:35:31
  • 275

Subtree of Another Tree问题及解法

问题描述: Given two non-empty binary trees s and t, check whether tree t has exactly the same structu...
  • u011809767
  • u011809767
  • 2017-05-15 10:56:29
  • 289

Subtree of Another Tree

问题描述判断一颗二叉树t是否是一颗二叉树s的 子树.分析:这种题目属于对二叉树的结构进行判断和操作的题目。注意题目说明的是判断子树问题,而不是子结构问题,也不是判断 同一颗二叉树问题. 子树—–>子...
  • u012280578
  • u012280578
  • 2017-05-07 15:03:53
  • 152

LeetCode.572 Subtree of Another Tree(树的序列化和子串的匹配)

题目: Given two non-empty binary trees s and t, check whether treet has exactly the same structure ...
  • xiakexiaohu
  • xiakexiaohu
  • 2018-01-23 15:28:36
  • 50
    个人资料
    持之以恒
    等级:
    访问量: 9293
    积分: 1102
    排名: 4万+
    文章分类
    文章存档