首页 > 代码库 > CC150 4.7
CC150 4.7
4.7 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.
// It will iterate main tree (millions) nodes. // Too bad. boolean isSubTree(Node main, Node sub) { if (main == null) return null; if (main == sub) return isTreeEquals(main, sub) if (main == sub) return isTreeEquals(sub, main); else { return isSubTree(main.left, sub) && isSubTree(main.right, sub); } } boolean isTreeEquals(Node t1, Node t2) { if (t1 == null || t2 == null) { return t1 == t2; } else { return ( t1 == t2 ) && isTreeEquals(t1.left, t2.left) && isTreeEquals(t2.right, t2.right); } }
Any better idea?
CC150 4.7
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。