Skip to content

Commit be937f8

Browse files
committed
08/09/2019
1 parent c6a8604 commit be937f8

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package DSA.Trees;
2+
3+
public class TreeToMirrorTree {
4+
static class node{
5+
int key;
6+
node left, right;
7+
8+
public node(int key) {
9+
this.key = key;
10+
left = right = null;
11+
}
12+
}
13+
14+
static void convert(node root){
15+
if(root ==null)
16+
return;
17+
18+
node temp = root.left;
19+
root.left = root.right;
20+
root.right = temp;
21+
22+
convert(root.left);
23+
convert(root.right);
24+
}
25+
26+
static void inorder(node root){
27+
if(root==null)
28+
return;
29+
inorder(root.left);
30+
System.out.print(root.key+" ");
31+
inorder(root.right);
32+
}
33+
34+
public static void main(String[] args) {
35+
node root = new node(1);
36+
root.left = new node(2);
37+
root.right = new node(3);
38+
root.left.left = new node(4);
39+
root.left.right = new node(5);
40+
41+
convert(root);
42+
inorder(root);
43+
}
44+
}

src/DSA/Trees/TreetoSumTree.java

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

0 commit comments

Comments
 (0)