This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bst; | |
/** | |
* Created by panded4 | |
*/ | |
public class CreateBSTMirror { | |
public static void main(String[] args) { | |
TNode n1 = new TNode(5); | |
n1.left = new TNode(10); | |
n1.right = new TNode(15); | |
n1.left.left = new TNode(20); | |
n1.left.right = new TNode(25); | |
n1.right.left = new TNode(30); | |
n1.right.right = new TNode(35); | |
inOrder(n1); | |
createMirror(n1); | |
System.out.println("\n"); | |
inOrder(n1); | |
} | |
private static void inOrder(TNode n1) { | |
if (n1 == null) | |
return; | |
inOrder(n1.left); | |
System.out.print(n1.data + " "); | |
inOrder(n1.right); | |
} | |
static TNode createMirror(TNode root) { | |
if (root == null) | |
return root; | |
TNode left = createMirror(root.left); | |
TNode right = createMirror(root.right); | |
root.left = right; | |
root.right = left; | |
return root; | |
} | |
} | |
class TNode { | |
int data; | |
TNode left, right; | |
public TNode(int data) { | |
left = right = null; | |
this.data = data; | |
} | |
} |