【easy】572. Subtree of Another Tree

判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等)

有两种方法:

方法二:递归写法

//方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树
//方法二:用递归来写十分的简洁,我们先从s的根结点开始,跟t比较,如果两棵树完全相同,那么返回true,否则就分别对s的左子结点和右子结点调用递归再次来判断是否相同,只要有一个返回true了,就表示可以找得到。

class Solution {
public:
    bool isSubtree(TreeNode* s, TreeNode* t) {
        //方法二的递归
        if (!s)
            return false;
        if (isSame(s,t))
            return true;
        return isSubtree(s->left,t) || isSubtree(s->right,t);
    }

    bool isSame(TreeNode* s,TreeNode* t){//这是一个子题,判断两个树是否相等
        if (s == NULL && t == NULL)
            return true;
        if (s == NULL || t == NULL)
            return false;
        if (s->val != t->val)
            return false;
        if (s->val == t->val)
            return isSame(s->left,t->left) && isSame(s->right,t->right);
    }
};

方法一:比较两个字符串

//写成两个序列,判断一个序列是否包含另一个序列为子序列
class Solution {
public:
    bool isSubtree(TreeNode* s, TreeNode* t) {
        ostringstream os1, os2;
        serialize(s, os1);
        serialize(t, os2);
        return os1.str().find(os2.str()) != string::npos;
    }
    void serialize(TreeNode* node, ostringstream& os) {
        if (!node) os << ",#";
        else {
            os << "," << node->val;
            serialize(node->left, os);
            serialize(node->right, os);
        }
    }
};

https://www.cnblogs.com/zfyouxi/p/4074592.html

介绍ostringstream.

原文地址:https://www.cnblogs.com/sherry-yang/p/8451975.html

时间: 2024-08-29 09:28:17

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

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

606. Construct String from Binary Tree 【easy】

606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be represented by empty parenthesis pair "()". And you

【DataStructure】Description and Introduction of Tree

[Description] At ree is a nonlinear data structure that models a hierarchical organization. The characteristic eatures are that each element may have several successors (called its "children") and every element except one (called the "root&

【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序

[BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose label is k-th largest in the subtree of the node x. Assume no two nodes have the same labels. Input

344. Reverse String【easy】

344. Reverse String[easy] Write a function that takes a string as input and returns the string reversed. Example:Given s = "hello", return "olleh". 解法一: 1 class Solution { 2 public: 3 string reverseString(string s) { 4 int start = 0, e

27. Remove Element【easy】

27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be ch

26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with consta

661. Image Smoother【easy】

661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surrounding cells and itself.

【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