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

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

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

每日一算法2019/6/12Day 40LeetCode572. Subtree of Another Tree

示例 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

Java 实现
TreeNode Class

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}
class Solution {
    public boolean isSubtree(TreeNode s, TreeNode t) {
        if (s == null) {
            return false;
        }
        if (isSame(s, t)) {
            return true;
        }
        return isSubtree(s.left, t) || isSubtree(s.right, t);
    }

    public boolean 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;
        }
        return isSame(s.left, t.left) && isSame(s.right, t.right);
    }
}

相似题目

  • 250. 统计同值子树 Count Univalue Subtrees
  • 508. 出现次数最多的子树元素和 Most Frequent Subtree Sum
  • 100. 相同的树 Same Tree

参考资料

原文地址:https://www.cnblogs.com/hglibin/p/11012726.html

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

LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40的相关文章

LeetCode 572. 另一个树的子树

题目链接:https://leetcode-cn.com/problems/subtree-of-another-tree/ 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 示例 1:给定的树 s: 3 / \ 4 5 / \ 1 2给定的树 t: 4 / \ 1 2返回 true,因为 t 与 s 的一个子树拥有相同的结构和节点值. 示例 2:给定的树 s: 3 /

572. 另一个树的子树

给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 示例 1:给定的树 s: 3 / \ 4 5 / \ 1 2给定的树 t: 4 / \ 1 2返回 true,因为 t 与 s 的一个子树拥有相同的结构和节点值 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/subtree-of-another-tree 1 pub

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 considere

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 considere

50. 树的子结构[subtree structure in tree]

[题目] 输入两棵二叉树A和B,判断B是不是A的子结构.二叉树结点的定义如下: C++ Code 123456   struct BinaryTreeNode {    int value;      BinaryTreeNode *left;      BinaryTreeNode *right; }; 如下图,右边的二叉树是左边二叉树的子结构. 请实现bool HasSubTree(BinaryTreeNode *parent, BinaryTreeNode *child)函数. [分析]

15.Subtree of Another Tree(判断一棵树是否为另一颗树的子树)

Level: ??Easy 题目描述: 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 cou

LeetCode算法题-Subtree of Another Tree(Java实现)

这是悦乐书的第265次更新,第278篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第132题(顺位题号是572).给定两个非空的二进制树s和t,检查树t是否具有完全相同的结构和具有子树s的节点值. s的子树是一个树,由s中的节点和所有节点的后代组成. 树也可以被视为自己的子树.例如: 鉴于树s: 3 / 4 5 / 1 2 鉴于树t: 4 / 1 2 返回true,因为t具有相同的结构和节点值,其子树为s. 鉴于树s: 3 / 4 5 / 1 2 / 0 鉴于树t:

Python3解leetcode 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

编程判断一个树是完全二叉树(使用层次遍历实现)

完全二叉树:一棵具有N个节点的二叉树的结构与满二叉树的前N个节点的结构相同 如何判断一个树是完全二叉树 可以使用层序遍历,只需2个步骤 第一步:如果遍历到一个节点只有右子树没有左子树,则不是完全二叉树 第二部:如果遍历到一个节点只有左子树,那么后面遍历到的节点必须是叶子节点,否则也不是完全二叉树 排除以上两种情况,则树是完全二叉树 核心代码: //层序遍历 int LayerOrder(BiTreeNode *head) { bool flag=0; LQueue Q; Initiate_Que