Repository navigation
Expand file tree
/
Copy pathMirror.java
More file actions
89 lines (70 loc) · 1.36 KB
/
Copy pathMirror.java
File metadata and controls
89 lines (70 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.io.*;
import java.util.*;
public class Mirror{
static class Node{
int data;
Node left;
Node right;
// constructor
Node(int data){
this.data = data;
this.left = null;
this.right = null;
}
}
static Node root;
static void create(){
Scanner sc = new Scanner(System.in);
Queue<Node> q = new LinkedList<>();
int x;
Node p,t;
System.out.println("Enter the root");
x = sc.nextInt();
root = new Node(x);
q.add(root);
while(!q.isEmpty()){
p = q.poll();
System.out.println("Enter left node of "+p.data);
x = sc.nextInt();
if(x!=-1){
t = new Node(x);
p.left = t;
q.add(t);
}
System.out.println("Enter right node of "+p.data);
x = sc.nextInt();
if(x!=-1){
t = new Node(x);
p.right = t;
q.add(t);
}
}
}
static void Mirror(Node n){
if(n==null){
return;
}
Mirror(n.left);
Mirror(n.right);
swap(n);
}
static void swap(Node root){
if(root == null){
return;
}
Node temp = root.left;
root.left = root.right;
root.right = temp;
}
static void pre(Node n){
if(n!=null){
System.out.print(n.data+" ");
pre(n.left);
pre(n.right);
}
}
public static void main(String[] args) {
create();
pre(root);
}
}