标签:interview
// 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?
标签:interview
原文地址:http://7371901.blog.51cto.com/7361901/1583682