Successfully reported this slideshow.
Your SlideShare is downloading. ×

Please help me flip this binary tree - PLEASE DON-'T MODIFY THE NODE.docx

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 3 Ad

Please help me flip this binary tree - PLEASE DON-'T MODIFY THE NODE.docx

Download to read offline

Please help me flip this binary tree
* PLEASE DON\'T MODIFY THE NODE CLASS
*/
static class Node {
Node(int data) {
this.data = data;
}
int data;
Node left;
Node right;
}

static void flip(Node root) {

// ****************** Your code here **********************

}

/*
Solution
public class MirrorTree {
public void mirror(Node root){
print(root);
Node x = flip(root);
System.out.print(\"\ Mirror Image \ \");
print(x);
}

public Node flip(Node root){
if(root!=null){
Node t = root.left;
root.left = root.right;
root.right = t;
flip(root.right);
flip(root.left);
}
return root;
}
public void print(Node root){
if(root!=null){
print(root.left);
System.out.print(\" \" + root.data);
print(root.right);
}
}
public static void main (String[] args) throws java.lang.Exception
{
Node root = new Node(4);
root.left = new Node(2);
root.right = new Node(6);
root.left.left = new Node(1);
root.left.right = new Node(3);
root.right.left = new Node(5);
root.right.right = new Node(7);
MirrorTree i = new MirrorTree();
i.mirror(root);
}
}
class Node{
int data;
Node left;
Node right;
public Node(int data){
this.data = data;
this.left = null;
this.right =null;
}
}
.

Please help me flip this binary tree
* PLEASE DON\'T MODIFY THE NODE CLASS
*/
static class Node {
Node(int data) {
this.data = data;
}
int data;
Node left;
Node right;
}

static void flip(Node root) {

// ****************** Your code here **********************

}

/*
Solution
public class MirrorTree {
public void mirror(Node root){
print(root);
Node x = flip(root);
System.out.print(\"\ Mirror Image \ \");
print(x);
}

public Node flip(Node root){
if(root!=null){
Node t = root.left;
root.left = root.right;
root.right = t;
flip(root.right);
flip(root.left);
}
return root;
}
public void print(Node root){
if(root!=null){
print(root.left);
System.out.print(\" \" + root.data);
print(root.right);
}
}
public static void main (String[] args) throws java.lang.Exception
{
Node root = new Node(4);
root.left = new Node(2);
root.right = new Node(6);
root.left.left = new Node(1);
root.left.right = new Node(3);
root.right.left = new Node(5);
root.right.right = new Node(7);
MirrorTree i = new MirrorTree();
i.mirror(root);
}
}
class Node{
int data;
Node left;
Node right;
public Node(int data){
this.data = data;
this.left = null;
this.right =null;
}
}
.

Advertisement
Advertisement

More Related Content

Similar to Please help me flip this binary tree - PLEASE DON-'T MODIFY THE NODE.docx (20)

More from rtodd19 (20)

Advertisement

Recently uploaded (20)

Please help me flip this binary tree - PLEASE DON-'T MODIFY THE NODE.docx

  1. 1. Please help me flip this binary tree * PLEASE DON'T MODIFY THE NODE CLASS */ static class Node { Node(int data) { this.data = data; } int data; Node left; Node right; } static void flip(Node root) { // ****************** Your code here ********************** } /* Solution public class MirrorTree { public void mirror(Node root){ print(root); Node x = flip(root); System.out.print(" Mirror Image "); print(x); } public Node flip(Node root){ if(root!=null){ Node t = root.left; root.left = root.right; root.right = t;
  2. 2. flip(root.right); flip(root.left); } return root; } public void print(Node root){ if(root!=null){ print(root.left); System.out.print(" " + root.data); print(root.right); } } public static void main (String[] args) throws java.lang.Exception { Node root = new Node(4); root.left = new Node(2); root.right = new Node(6); root.left.left = new Node(1); root.left.right = new Node(3); root.right.left = new Node(5); root.right.right = new Node(7); MirrorTree i = new MirrorTree(); i.mirror(root); } } class Node{ int data; Node left; Node right; public Node(int data){ this.data = data; this.left = null; this.right =null; } }

×