Amazon.com Interview Question
1,589 Interview Reviews |
Back to all Amazon.com Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Development Engineer Intern at Amazon.com:
Write a program that sees if two binary trees are equal.
See more for this Amazon.com Software Development Engineer Intern Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (6)
<?php
class node{
public $left;
public $right;
public $value;
function _construct($value=null,$left=null,$right=null)
{
$this->value = $value;
$this->left = $left;
$this->right = $right;
}
// O(n) times inorder traversal
function testEsqual($tree1,$tree2)
{
if($tree1->value ==null || $tree2->value==null)
return false;
if($tree1->value ==null && $tree2->value==null)
return true;
while($tree1->value!=null)
{
if($tree1->value == $tree2->value)
{
equal($tree1->left,$tree2->left);
equal($tree1->right,$tree2->right);
}
else
{
return false;
}
}
}
}
?>
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
2 of 2 people found this helpful
{
if(
(node1 == NULL && node2 != NULL) ||
(node2 == NULL && node1 != NULL )
return false;
if(node1 == NULL && node2 == NULL)
return true;
if(node1->data != node2->data)
return false;
return( AreEqual(node1->left, node2->left) && AreEqual( node1->right, node2->right)
};
int main();
{
AreEqual(root1, root);
};
Helpful Answer?
Yes |
No
Inappropriate?
An additional if is required.
Helpful Answer?
Yes |
No
Inappropriate?
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 0 people found this helpful
by Interview Candidate: