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.
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
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:
/**
* 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 T1, T2: The roots of binary tree.
* @return: True if T2 is a subtree of T1, or false.
*/
public boolean isSubtree(TreeNode T1, TreeNode T2) {
// write your code here
if(T2 == null){
return true;
}
if(T1 == null){
return false;
}
return isSame(T1,T2) || isSubtree(T1.left, T2) || isSubtree(T1.right, T2);
} private boolean isSame(TreeNode T1, TreeNode T2){
if(T1 == null && T2 == null){
return true;
}
if(T1 == null || T2 == null){
return false;
}
if(T1.val != T2.val){
return false;
}
return isSame(T1.left, T2.left) && isSame(T1.right, T2.right);
}
}
LintCode Subtree的更多相关文章
- lintcode:Subtree 子树
题目: 子树 有两个不同大小的二叉树: T1 有上百万的节点: T2 有好几百的节点.请设计一种算法,判定 T2 是否为 T1的子树. 样例 下面的例子中 T2 是 T1 的子树: 1 3 / \ / ...
- 245. Subtree【LintCode java】
Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- Lintcode245 Subtree solution 题解
[题目描述] You have two every large binary trees:T1, with millions of nodes, and T2, with hundreds of no ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- LintCode题解之子树
思路: 最简单的方法,依次遍历比较就可以了. AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int va ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- lintcode算法周竞赛
------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...
随机推荐
- Google Code Jam 2009 Qualification Round Problem C. Welcome to Code Jam
本题的 Large dataset 本人尚未解决. https://code.google.com/codejam/contest/90101/dashboard#s=p2 Problem So yo ...
- Java_获取当前月最后一天
List<String> ms = DateUtils.getMonths(7,"yyyyMM"); SimpleDateFormat sdf = new Simple ...
- Xshell选中的同时把内容复制到剪贴板(还可以设置设置文本分隔符)
1.设置对话框 工具 -> 选项 -> 键盘和鼠标 -> 将选定的文本自动复制到剪贴板 2.贴图如下 2.1.打开设置对话框 2.2.设置键盘鼠标,左键复制 2.3.右键粘贴 作者: ...
- Repeater控件中的三目运算
<asp:Repeater ID="rptimg" runat="server"> <ItemTemplate> ...
- Html - Iframe
父页面调用子页面 //用这个对象调用子页面的函数或者dom var myiframe = $("#right_iframe")[0].contentWindow; 子页面调用父页面 ...
- [转]3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】
本文转自:http://www.cnblogs.com/jirigala/archive/2010/10/07/1845275.html 2010-10-07 21:39 by 通用C#系统架构, 5 ...
- Asp.Net:Repeater 详情 备用
页面 repeator就想for循环一样,没有编辑模板,有删除delete和详情detail模板 <%@ Page Language="C#" AutoEventWireup ...
- nginx不支持pathinfo函数
server { listen ; server_name www.domain.com domain.com; error_page /.html; error_page /50x.html; lo ...
- Frenetic QuickInstall
Frenetic a family of network programming languages 官方网站:Frenetic Github:Frenetic QuickInstall 第一步,先安 ...
- TabBarViewController的创建以及渐变隐藏
// CustomTabBarViewController.h #import <UIKit/UIKit.h> @interface CustomTabBarViewController ...