Amazon Interview Question
InternsCountry: United States
a non-recursive way:
template <typename T>
void BinaryTree<T>::mirror() {
queue<BinaryTreeNode<T> *> q;
if (root) {
q.push(root);
while (!q.empty()) {
BinaryTreeNode<T> *front = q.front();
q.pop();
BinaryTreeNode<T> *temp = front->getLeftChild();
front->setLeftChild(front->getRightChild());
front->setRightChild(temp);
if (front->getLeftChild()) {
q.push(front->getLeftChild());
}
if (front->getRightChild()) {
q.push(front->getRightChild());
}
}
}
}
void mirror()
{
trees temp = left;
left = right;
right = temp;
if(left!=null)
left.mirror();
if(right!=null)
right.mirror();
}
/*
* create a mirrored tree
*/
tree_node_t* tree_create_mirror (tree_node_t* root)
{
if (root == NULL) {
return (NULL);
}
tree_node_t *new_root = (tree_node_t*)malloc(sizeof(tree_node_t));
if (new_root == NULL) {
printf("tree node malloc failure.\n");
return (NULL);
}
new_root->value = root->value;
new_root->left = tree_create_mirror(root->right);
new_root->right = tree_create_mirror(root->left);
return (new_root);
}
void TreeOperation::mirrorTree(TreeNode* node){
if(node != NULL){
mirrorTree(node->getLeftChild());
mirrorTree(node->rightChild);
swapChildren(node);
}
}
/**
* Swap the children of a given parent.
* Works only for binary tree.
*/
void TreeOperation::swapChildren(TreeNode* parent){
TreeNode* temp;
temp=parent->getLeftChild();
parent->setLeftChild(parent->rightChild);
parent->rightChild = temp;
}
template<typename T>
struct node_traits
{
typedef typename T type_value
typedef typename const T type_value_const;
typedef typename T& type_ref;
typedef typename const T& type_ref_const;
typedef typename T&& type_rref;
typedef typename const T&& type_rref_const;
typedef typename T* type_ptr;
typedef typename const T* type_ptr_const; // pointer to const
typedef typename const T* const type_const_ptr_const; // const pointer to const
typedef typename T* const type_const_ptr; // constant pointer to non-const
typedef std::shared_ptr<node> link;
};
template<typename T>
struct node
{
public :
typedef node_traits<T>::type_value type_value;
node(const T& x) : item(x) { l.reset(); r.reset(); }
private :
T item;
std::shared_ptr<node> l, r;
};
template<typename T>
class bst
{
public :
typedef node_traits<T>::link link;
friend void swap(link a, link b);
bst() { head.reset(); }
// ...
link mirror()
{
return mirrorR(head);
}
private :
// ...
link mirrorR(link h)
{
if(h == 0) return std::nullptr;
mirrorR(h->l);
mirrorR(h->r);
swap(h->l, h->r);
}
link head;
}
void swap(link a, link b)
{
link t(a);
a = b;
b = t;
}
package tree;
class TreeNode{
int data;
TreeNode leftNode;
TreeNode rightNode;
public TreeNode(int d){
this.data = d;
this.leftNode = null;
this.rightNode = null;
}
}
public class MirrorTree {
TreeNode root;
TreeNode currentNode;
int sum;
public MirrorTree(){
this.sum = 0;
this.root = null;
this.currentNode = null;
}
public boolean compareNodeValue(int currentNode, int newNode){
if(currentNode < newNode){
return true;
}
else{
return false;
}
}
public void add(int value){
TreeNode newNode = new TreeNode(value);
//newNode.data = value;
if(root == null){
root = newNode;
newNode = null;
currentNode = root;
}else{
currentNode = root;
if(compareNodeValue(currentNode.data, newNode.data)){
addRight(newNode);
}
else{
addLeft(newNode);
}}
}
public void addRight(TreeNode newRNode){
if(currentNode.rightNode == null){
currentNode.rightNode = newRNode;
newRNode = null;
}
else{
currentNode = currentNode.rightNode;
if(compareNodeValue(currentNode.data, newRNode.data)){
addRight(newRNode);
}
else{
addLeft(newRNode);
}
}
}
public void addLeft(TreeNode newLNode){
if(currentNode.leftNode == null){
currentNode.leftNode = newLNode;
newLNode = null;
}
else{
currentNode = currentNode.leftNode;
if(compareNodeValue(currentNode.data, newLNode.data)){
addRight(newLNode);
}
else{
addLeft(newLNode);
}
}
}
public void displayInorder(TreeNode rootNode) {
if (rootNode != null) {
displayInorder(rootNode.leftNode);
System.out.print(" " + rootNode.data);
displayInorder(rootNode.rightNode);
}
}
private void mirror(TreeNode node) {
if (node != null) {
mirror(node.leftNode);
mirror(node.rightNode);
TreeNode temp = node.leftNode;
node.leftNode = node.rightNode;
node.rightNode = temp;
}
}
public static void main(String[] args) {
MirrorTree obj = new MirrorTree();
obj.add(10);
obj.add(20);
obj.add(15);
obj.add(25);
obj.displayInorder(obj.root);
System.out.println(" ");
obj.mirror(obj.root);
System.out.println(" ");
System.out.println("Mirror Tree");
obj.displayInorder(obj.root);
}
}
- Kabeer August 10, 2013