-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathMirror a BST
More file actions
64 lines (53 loc) · 1.34 KB
/
Copy pathMirror a BST
File metadata and controls
64 lines (53 loc) · 1.34 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
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);
}
}