Skip to content

Commit 86cd426

Browse files
committed
convert tree to its mirror
1 parent 923ce8a commit 86cd426

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Trees/mirrorConvert.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Node
2+
{
3+
int data;
4+
Node left;
5+
Node right;
6+
Node(int data)
7+
{
8+
this.data=data;
9+
left=right=null;
10+
}
11+
}
12+
13+
class BinaryTree{
14+
Node root;
15+
BinaryTree()
16+
{
17+
root = null;
18+
}
19+
BinaryTree(int data)
20+
{
21+
this.root=new Node(data);
22+
}
23+
Node mirror(Node root)
24+
{
25+
if(root==null)
26+
{
27+
return null;
28+
}
29+
Node temp=root.left;
30+
root.left=root.right;
31+
root.right=temp;
32+
mirror(root.left);
33+
mirror(root.right);
34+
return root;
35+
}
36+
}
37+
38+
class mirrorConvert{
39+
public static void main(String[] args) {
40+
41+
BinaryTree bt=new BinaryTree(2);
42+
bt.root.left=new Node(3);
43+
bt.root.right=new Node(5);
44+
bt.root.left.right=new Node(9);
45+
bt.root.right.left=new Node(7);
46+
Node temp=bt.mirror(bt.root);
47+
System.out.println(temp.data+" "+temp.left.data+" "+temp.right.data);
48+
49+
}
50+
}

0 commit comments

Comments
 (0)