美文网首页
leetcode--572--另一个树的子树

leetcode--572--另一个树的子树

作者: minningl | 来源:发表于2020-11-24 23:39 被阅读0次

题目:
给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。

示例 1:
给定的树 s:

     3
    / \
   4   5
  / \
 1   2

给定的树 t:

   4 
  / \
 1   2

返回 true,因为 t 与 s 的一个子树拥有相同的结构和节点值。

示例 2:
给定的树 s:

     3
    / \
   4   5
  / \
 1   2
    /
   0

给定的树 t:

   4
  / \
 1   2

返回 false。

链接:https://leetcode-cn.com/problems/subtree-of-another-tree

思路1:
1、判断A是否是B的子树,则可以判断A是否等于B 或者 A是否和B的左右子树相等
2、定义一个函数判断两棵树是否相等,即递归的判断两个数的当前节点和左右子树是否相同

Python代码:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):

    def isSame(self, x, y):
        if not x and not y:
            return True
        if not x and y:
            return False
        if x and not y:
            return False
        if x.val != y.val:
            return False
        else:
            return self.isSame(x.left, y.left) and self.isSame(x.right, y.right)

    def isSubtree(self, s, t):
        """
        :type s: TreeNode
        :type t: TreeNode
        :rtype: bool
        """
        if not s and not t:
            return True
        if not s and t:
            return False
        return self.isSame(s, t) or self.isSubtree(s.left, t) or self.isSubtree(s.right, t)

C++代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    bool isSame(TreeNode* s, TreeNode* t){
        if (s==nullptr && t==nullptr){
            return true;
        }
        return s && t && s->val==t->val && isSame(s->left,t->left) && isSame(s->right, t->right);
    }

    bool isSubtree(TreeNode* s, TreeNode* t) {
        if (s==nullptr && t==nullptr){
            return true;
        }
        if (s==nullptr && t!=nullptr){
            return false;
        }
        return isSame(s,t) || isSubtree(s->left,t) || isSubtree(s->right,t);
    }
};

思路2:
1、一个取巧的做法,将两个树都转化为字符串,判断树A对应的字符串是否在树B对应的字符串中,以此来判断A是否是B的子树

Python代码:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):

    def isSubtree(self, s, t):
        """
        :type s: TreeNode
        :type t: TreeNode
        :rtype: bool
        """
        return str(t) in str(s)

相关文章

  • leetcode--572--另一个树的子树

    题目:给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 ...

  • [Leetcode] 572. 另一个树的子树

    572. 另一个树的子树 来源: 572. 另一个树的子树 1. 题目描述 给定两个非空二叉树 s 和 t,检验...

  • 另一个树的子树

    题目: 题目的理解: 找从t开始所有节点的值都一样的子节点。 python实现 想看最优解法移步此处 提交 刚刚还...

  • 另一个树的子树

    题目描述 https://leetcode-cn.com/problems/subtree-of-another-...

  • 另一个树的子树

    标签:树 dfs 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一...

  • LeetCode 572. 另一个树的子树 | Python

    572. 另一个树的子树 题目来源:https://leetcode-cn.com/problems/subtre...

  • 树、二叉树

    树的概念 节点、根节点、父节点、子节点、兄弟节点 、子树、左子树、右子树、空树 节点的度(degree):子树的个...

  • 树--(二叉树、B树、B+树)

    二叉树 二分法形成的树右子树大于左子树,长于左子树平衡二叉树:右子树比左子树高度差不超过1 算法效率 O(logN...

  • 数据结构与算法(二叉树)

    二叉树 二叉树的基本概念 二叉树是每个节点最多有两个子树的树结构,通常子树被称作“左子树”和“右子树”。 二叉树的...

  • 刷题No8 二叉树基本概念

    二叉树是每个结点最多有两个子树的有序树。通常子树的根被称作“左子树”(left subtree)和“右子树”(ri...

网友评论

      本文标题:leetcode--572--另一个树的子树

      本文链接:https://www.haomeiwen.com/subject/unrpiktx.html