SubTree

You have two every large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1.

Notice

A tree T2 is a subtree of T1 if there exists a node n in T1 such that the subtree of n is identical to T2. That is, if you cut off the tree at node n, the two trees would be identical.

这题思路很明显,先找T1中和T2头结点一样的结点,找到这个节点后,如果这个节点构成能的子树和T2一样,则T2是T1的subtree。

但是需要注意的是:树中结点的值可能有重复,所以找到一个之后,如果还不是subtree,需要继续寻找。这个非常重要!!

代码如下:

"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""
class Solution:
    # @param T1, T2: The roots of binary tree.
    # @return: True if T2 is a subtree of T1, or false.
    def isSubtree(self, T1, T2):
        result = False
        if not T2:
            return True
        if not T1 and T2:
            return False
        if T1.val == T2.val:
            result = self.isSameTree(T1, T2)
        if not result:
            result = self.isSubtree(T1.left, T2)
        if not result:
            result = self.isSubtree(T1.right, T2)
        return result 

    def isSameTree(self, T1, T2):
        if not T1 and not T2:
            return True
        if (not T1 and T2) or (T1 and not T2):
            return False
        if T1.val != T2.val:
            return False
        else:
            return self.isSameTree(T1.left, T2.left) and self.isSameTree(T1.right,T2.right)
            
时间: 2024-12-16 05:11:01

SubTree的相关文章

[LeetCode] Largest BST Subtree 最大的二分搜索子树

Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note:A subtree must include all of its descendants.Here's an example: 10 / 5 15 / \ \ 1 8 7 The Largest

Minimum Subtree

我的错误代码 理递归思路 /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root

lintcode 容易题:Subtree 子树

题目: 子树 有两个不同大小的二叉树: T1 有上百万的节点: T2 有好几百的节点.请设计一种算法,判定 T2 是否为 T1的子树. 样例 下面的例子中 T2 是 T1 的子树: 1 3 / \ / T1 = 2 3 T2 = 4 / 4 下面的例子中 T2 不是 T1 的子树: 1 3 / \ T1 = 2 3 T2 = 4 / 4 注意 若 T1 中存在从节点 n 开始的子树与 T2 相同,我们称 T2 是 T1 的子树.也就是说,如果在 T1 节点 n 处将树砍断,砍断的部分将与 T2

git subtree:无缝管理通用子项目

移动互联网的爆发以及响应式页面的尴尬症,开发web和mobile项目成为了标配,当然实际情况下,会有更多的项目. 多项目开发对于前端来说是个很大的挑战? 重复,重复的前端架构,重复的前端依赖,重复的工具函数等? 局限,不同后台有不同的规则,"因地制宜"真难受,刚伺候好rails又突然来个php? 最优,后台工程做前端构建,总是显得不够"最优". 所以,我们需要单独抽离出前端开发项目,按照前端的方式来组织代码,通过构建工具来对前端资源文件做最优处理那么新问题来了,如何

333.Largest BST Subtree

/* * 333.Largest BST Subtree * 2016-3-27 by Mingyang * 这个题目我的思路,自底向上的方法非常正确的!但是,这个题目独特的一点就在于对于一个 * Tree是不是BST得判断,他必须表示对左子树的最大的还大,右子树的最小的还要小,所以这样看来就是 * 必须要保证root必须保存当前子树1.isBST?2.left smallest.3.right biggest.4.node number * 可以先建一个class,也可以做一个array */

使用GIT SUBTREE集成项目到子目录(转)

原文:http://aoxuis.me/post/2013-08-06-git-subtree 使用场景 例如,在项目Game中有一个子目录AI.Game和AI分别是一个独立的git项目,可以分开维护.为了避免直接复制粘贴代码,我们希望Game中的AI子目录与AI的git项目关联,有3层意思: AI子目录使用AI的git项目来填充,内容保持一致. 当AI的git项目代码有更新,可以拉取更新到Game项目的AI子目录来. 反过来,当Game项目的AI子目录有变更,还可以推送这些变更到AI的git项

LintCode Subtree

原题链接在这里:http://www.lintcode.com/en/problem/subtree/ You have two every large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree ofT1. Have you met this question in a real intervi

LeetCode Largest BST Subtree

原题链接在这里:https://leetcode.com/problems/largest-bst-subtree/ Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note:A subtree must include all of its desce

508. Most Frequent Subtree Sum (Medium)

Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most frequent

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