From 35e8c6ee9f2446f0a3c3ce3fe84a8d2978cdfb72 Mon Sep 17 00:00:00 2001 From: binodrai9 <110383592+binodrai9@users.noreply.github.com> Date: Sun, 18 Dec 2022 21:55:41 +0530 Subject: [PATCH] Create Mirror a BST --- Mirror a BST | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Mirror a BST diff --git a/Mirror a BST b/Mirror a BST new file mode 100644 index 0000000..6c7ad6e --- /dev/null +++ b/Mirror a BST @@ -0,0 +1,64 @@ +public class MirroraBST{ + static class Node{ + int data; + Node left; + Node right; + + public Node (int data){ + this.data = data; + this.left = this.right = null; + } + + } + public static Node createMirror(Node root){ + if(root==null){ + return null; + } + Node leftMirror = createMirror(root.left); + Node rightMirror = createMirror(root.right); + + root.left = rightMirror; + root.right = leftMirror; + return root; + } + public static void preorder(Node root){ + if(root==null){ + return; + } + System.out.print(root.data + " "); + preorder(root.left); + preorder(root.right); + } + public static void main(String args[]){ + /* + 8 + / \ + 5 10 + / \ \ + 3 6 11 + + to + + 8 + / \ + 10 5 + / / \ + 11 6 3 + mirror bst + + + */ + + Node root = new Node(8); + root.left = new Node(5); + root.right = new Node(10); + root.left.left = new Node(3); + root.left.right = new Node(6); + root.right.right = new Node(11); + + root = createMirror(root); + preorder(root); + + + } +}