【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.

解题思路:我的方法很简单,用先序遍历的方法分别遍历这两棵树,并记录起遍历的路径,最后判断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

原文地址:https://www.cnblogs.com/seyjs/p/10585867.html

时间: 2024-08-12 20:54:06

【leetcode】572. Subtree of Another Tree的相关文章

【easy】572. Subtree of Another Tree

判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等) 有两种方法: 方法二:递归写法 //方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树 //方法二:用递归来写十分的简洁,我们先从s的根结点开始,跟t比较,如果两棵树完全相同,那么返回true,否则就分别对s的左子结点和右子结点调用递归再次来判断是否相同,只要有一个返回true了,就表示可以找得到. class Solution { public: bool isSubtree(TreeNode

【LeetCode】 Maximum Depth of Binary Tree

Maximum Depth of Binary Tree  Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

【LeetCode】98. Validate Binary Search Tree

Difficulty:Medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/validate-binary-search-tree/ Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a nod

【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 that duplicates do not exist in the tree. 提示: 题目要求通过一颗二叉树的中序遍历及后续遍历的结果,将这颗二叉树构建出来,另外还有一个已知条件,所有节点的值都是不同的. 首先需要了解一下二叉树不同遍历方式的定义: 前序遍历:首先访问根结点,然后遍历左子树,最

【LeetCode】98. Validate Binary Search Tree -判断是否为二叉排序树

一.描述: 二.思路: 二叉排序树(BST),中序遍历的结果一定是非递减序列(来自百度百科): 本题中对于BST的定义是要么大于,要么小与,即遍历结果只能是递增序列,故可以通过判断中序遍历的结果序列是否是递增序列,来判断是否为合法BST: 另一种方法是使用递归: 三.代码: 1.非递归,通过中序遍历结果判断: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left;

【leetcode】Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 利用递归. 也可以使用栈和队列,即使用DFS和BFS方法. C++: 1 /** 2 * Definition for binary tree 3 * struct TreeNod

【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal

Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and Postorder' (Problem 106), u need build the binary tree. Input: 105. Preorder & Inorder traversal 106. Inorder & Postorder traversal output: A binar

【LeetCode】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys