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 interview?

Yes

Example

T2 is a subtree of T1 in the following case:

       1                3
      / \              /
T1 = 2   3      T2 =  4
        /
       4

T2 isn‘t a subtree of T1 in the following case:

       1               3
      / \               T1 = 2   3       T2 =    4
        /
       4

Note

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.

Time Complexity: O(m*n), m是T1的node数, n 是T2的node 数. Space: O(logm). isSame用logn, isSubtree用logm.

AC Java:

 1 /**
 2  * Definition of TreeNode:
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left, right;
 6  *     public TreeNode(int val) {
 7  *         this.val = val;
 8  *         this.left = this.right = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     /**
14      * @param T1, T2: The roots of binary tree.
15      * @return: True if T2 is a subtree of T1, or false.
16      */
17     public boolean isSubtree(TreeNode T1, TreeNode T2) {
18         // write your code here
19         if(T2 == null){
20             return true;
21         }
22         if(T1 == null){
23             return false;
24         }
25         return isSame(T1,T2) || isSubtree(T1.left, T2) || isSubtree(T1.right, T2);
26     }
27
28     private boolean isSame(TreeNode T1, TreeNode T2){
29         if(T1 == null && T2 == null){
30             return true;
31         }
32         if(T1 == null || T2 == null){
33             return false;
34         }
35         if(T1.val != T2.val){
36             return false;
37         }
38         return isSame(T1.left, T2.left) && isSame(T1.right, T2.right);
39     }
40 }
时间: 2024-10-09 23:16:19

LintCode Subtree的相关文章

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

245. Subtree【LintCode java】

Description You have two very 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. A tree T2 is a subtree of T1 if there exists a node n in T1 such that the subtree of

[LintCode] 597. Subtree with Maximum Average

Given a binary tree, find the subtree with maximum average. Return the root of the subtree. Example 1 Input: {1,-5,11,1,2,4,-2} Output:11 Explanation: The tree is look like this: 1 / -5 11 / \ / 1 2 4 -2 The average of subtree of 11 is 4.3333, is the

[lintcode the-smallest-difference]最小差(python)

题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回最小差. 排好序后用两个指针分别扫描两个数组,每次更新他们的差值的绝对值.并且依据他们两个数字的大小来决定谁来移动指针. 1 class Solution: 2 # @param

[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

lintcode.44 最小子数组

最小子数组 描述 笔记 数据 评测 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 注意事项 子数组最少包含一个数字 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Airbnb Amazon LinkedIn Cryptic Studios Dropbox Apple Epic Systems TinyCo Yelp Hedvig Zenefits Uber Snapchat Yahoo Microsoft Bloomberg Facebook Google

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 66.67.68 二叉树遍历(前序、中序、后序)

AC代码: /** * 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: The r

[LintCode/LeetCode]——两数和、三数和、四数和

LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号:56,链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 题目描述: 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 1