LintCode: isSubTree】的更多相关文章

Title: 有两个不同大小的二进制树: T1 有上百万的节点: T2 有好几百的节点.请设计一种算法,判定 T2 是否为 T1的子树 class Solution { public: /** * @param T1, T2: The roots of binary tree. * @return: True if T2 is a subtree of T1, or false. */ bool isSubtree(TreeNode *T1, TreeNode *T2) { bool resul…
原题链接在这里: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…
思路: 最简单的方法,依次遍历比较就可以了. 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 { /…
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (2016-02-10) For more problems and solutions, you can see my LintCode repository. I'll keep updating for full summary and better solutions. See cnblogs t…
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是简单的剖析思路以及不能bug-free的具体细节原因. ---------------------------------------------------------------- ------------------------------------------- 第九周:图和搜索. ---…
-------------------------------------------- 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; * } * } */ pub…
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩下一个元素的时候(事实上只要满足一个元素出现过半就一定会剩下一个元素的)这个元素就是我们要找的数了. AC代码: public class Solution { /** * @param nums: a list of integers * @return: find a majority numb…
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } */ pu…
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还是只做出了O(n^2)... 勉强AC代码: public class Solution { /** * @param str: a string * @return: a boolean */ public boolean isUnique(String s) { for(int i=0;i<s.…
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left…