is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.
CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.
Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.
Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.
Do inorder traversal for both tree and keep the output in a queue. When trying to add item from inorder of first tree check if output from second queue is empty. If queue is not empty, remove that item from second queue.
- nikunj.iitk May 02, 2015Worst case O(m + n) : Both tree fully traversed and Best case: O(max(log m, log n)) : First item in the tree don't match.
Code:
public <T> boolean inorder(Util.Tree<T> t1, Util.Tree<T> t2, Queue<T> res1, Queue<T> res2) {
if (t1 == null && t2 == null) {
return true;
}
if (!inorder((t1 != null ? t1.left : null), (t2 != null ? t2.left : null), res1, res2)) {
return false;
}
if (t1 != null) {
if (!add(t1.value, res1, res2)) {
return false;
}
}
if (t2 != null) {
if (!add(t2.value, res2, res1)) {
return false;
}
}
if (!inorder((t1 != null ? t1.right : null), (t2 != null ? t2.right : null), res1, res2)) {
return false;
}
return true;
}
public <T> boolean solve(Util.Tree<T> t1, Util.Tree<T> t2) {
LinkedTransferQueue<T> res1 = new LinkedTransferQueue<T>();
LinkedTransferQueue<T> res2 = new LinkedTransferQueue<T>();
return inorder(t1, t2, res1, res2) && res1.isEmpty() && res2.isEmpty();
}